簡體   English   中英

如何在事件電視馬拉松、電報中發送消息?

[英]How to send a message inside an event telethon, telegram?

我寫了一個 function,它在消息寫入電報頻道時開始。 如果消息是"start" ,則 function 打印 1,而如果收到"stop"消息,則設置條件並導致線程中的循環停止。 然后它應該通過 Telethon 發送消息,但我收到一個錯誤:

from threading import Event, Thread
from telethon import TelegramClient, sync
import telebot
condition = Event()

api_id = ******

api_hash = '*******'

client = TelegramClient('my_session', api_id, api_hash)
client.start()

# telegram bot
token_1_bot = '5******'
chat_id = -********



bot_1 = telebot.TeleBot(token_1_bot)


def do_smth():
    while not condition.is_set():
        print(1)

    client.send_message('@bots_username', 'Hi bot you are bot')
    print(2)

@bot_1.message_handler(content_types='text')
def on_message(message):
    if "start" in message.text:

        # open documents page
        # Thread(target=asyncio.run, args=(application_fill(id, channel),)).start()
        Thread(target=do_smth, args=()).start()

    elif message.text == 'stop':
        condition.set()


bot_1.infinity_polling()

我得到的錯誤:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "F:/aktaustroyexpert/tel_test.py", line 50, in do_smth
    client.send_message('@bot_username', 'Hi bot you are bot')
  File "C:\Python\Python37\lib\site-packages\telethon\sync.py", line 35, in syncified
    loop = asyncio.get_event_loop()
  File "C:\Python\Python37\lib\asyncio\events.py", line 644, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.

C:\Python\Python37\lib\threading.py:951: RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited
  del exc_type, exc_value, exc_tb

怎么了? 如何從一個簡單的函數(通過 Telethon)向機器人發送消息(收到“開始”消息時調用此 function)?

所以我的主要任務是從打開 URL 的計算機向機器人發送stop消息,以使其他機器人停止打開 URL。

await client.send_message('@bot_username', 'Hi bot you are bot')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM