Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Telegram 如何使用非电报事件触发器发送telethon消息_Telegram_Telethon - Fatal编程技术网

Telegram 如何使用非电报事件触发器发送telethon消息

Telegram 如何使用非电报事件触发器发送telethon消息,telegram,telethon,Telegram,Telethon,我正试图用telethon发一条电报信息,这时一个按钮触发了我 当被NewMessage事件等事件触发时,我的telethon方法可以正常工作,但如何使用其他触发器(即按下按钮,telethon发送消息)发送消息(客户端。发送消息(用户,消息)) 目前我得到的只是以下错误: RuntimeError: There is no current event loop in thread 'Thread-1'. RuntimeWarning: coroutine 'send_to' was neve

我正试图用telethon发一条电报信息,这时一个按钮触发了我

当被NewMessage事件等事件触发时,我的telethon方法可以正常工作,但如何使用其他触发器(即按下按钮,telethon发送消息)发送消息(
客户端。发送消息(用户,消息)

目前我得到的只是以下错误:

RuntimeError: There is no current event loop in thread 'Thread-1'.

RuntimeWarning: coroutine 'send_to' was never awaited
以下是我的代码的简化版本:

with client:
      client.start()
      while True:
         if (button):
         await client.send_message(int(chat),msg)
      client.run_until_disconnected()
编辑:

事后看来,我原来的问题过于简单了。我没有使用按钮,但语音命令,不管怎样,都是非电报触发器。在电报聊天组@TelethonChat的帮助下,答案是:

import asyncio

loop = asyncio.new_event_loop()

async def send_to(chat, msg):
    await client.send_message(chat, msg)

def mainfunc():
    if (trigger):
        loop.create_task(send_to(chat, msg))

您需要使用事件来实现这一点。单击按钮的事件是
events.CallbackQuery

示例代码如下所示:

from telethon import events
from telethon.tl.custom import Button

@client.on(events.CallbackQuery)
async def handler(event):
    await event.answer('You clicked {}!'.format(event.data))

client.send_message(chat, 'Pick one', buttons=[
    [Button.inline('Left'), Button.inline('Right')]]
])
您可以在此处找到更多示例: