我的机器人代码中有两个处理程序:
my_conversation - 捕获“/start”消息并开始新对话,其中等待来自用户的消息
数字 - 按模式捕获消息 - 仅数字
import asyncio
import logging
import re
from telethon import TelegramClient
from telethon.events import StopPropagation, NewMessage
me = TelegramClient('bot', 'API_ID_BOT', 'API_HASH_BOT').start(bot_token='BOT_TOKEN')
async def my_conversation(event):
async with me.conversation(event.sender_id) as conv:
await conv.send_message('I\'m waiting for message')
response = conv.get_response()
response = await response
await conv.send_message(f'conversation: {response.text}')
raise StopPropagation
async def digits(event):
await me.send_message(event.sender_id, f'catches digits: {event.text}')
raise StopPropagation
async def main():
me.add_event_handler(my_conversation, NewMessage(incoming=True, pattern=r'^\/start$'))
me.add_event_handler(digits, NewMessage(incoming=True, pattern=re.compile(r'[0-9]+')))
await me.run_until_disconnected()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
我的期望:
我发送“/start”
机器人开始对话并回复“我正在等待消息”
我发送“123”
机器人发送消息“对话:123”,因为对话已开始。其他处理程序必须忽略消息,因为对话已经开始。
我有什么:
机器人发送消息“捕获数字:123”
机器人发送消息“对话:123”
所以 Bot 也在对话之外捕获了处理程序的消息,太出乎意料了。我必须在脚本中更改哪些内容才能使其正常工作?
慕标5832272
相关分类