如何将 ConversationHandler 模块从 Python-Telegram-Bot 迁移到 Telethon [英] How to migrate ConversationHandler module from Python-Telegram-Bot to Telethon
问题描述
Python-telegram-bot这是 HTTP Telegram Bot API 包装器具有 telegram.ext.ConversationHandler 模块及其功能是:通过管理其他处理程序的四个集合来与单个用户进行对话的处理程序."
我正在从这个 python-telegram-bot 迁移到 Telethon MTProtoAPI.我有这个 ConversationHandler
来管理对话.如何在 Telethon 中创建任何类型的 ConversationHandler
.
I'm migrating from this python-telegram-bot to Telethon MTProto API. And I have this ConversationHandler
to manage conversation. How can I create any type of ConversationHandler
in Telethon.
以下是 Telethon 给 从 python-telegram-bot 迁移.他们使用 echobot2.py来自ptb的例子.如何使用此示例进行迁移 conversationbot.py.
Here is some little overview given by Telethon to migrate from python-telegram-bot. They use echobot2.py from ptb's examples. How can I migrate using this example conversationbot.py.
推荐答案
您可以轻松创建所谓的有限状态机";(FSM) 能够区分用户所处的对话的不同状态.
You can easily create what's known as a "finite state machine" (FSM) that is able to differentiate between the different states of a conversation a user can find themselves in.
from enum import Enum, auto
# We use a Python Enum for the state because it's a clean and easy way to do it
class State(Enum):
WAIT_NAME = auto()
WAIT_AGE = auto()
# The state in which different users are, {user_id: state}
conversation_state = {}
# ...code to create and setup your client...
@client.on(events.NewMessage)
async def handler(event):
who = event.sender_id
state = conversation_state.get(who)
if state is None:
# Starting a conversation
await event.respond('Hi! What is your name?')
conversation_state[who] = State.WAIT_NAME
elif state == State.WAIT_NAME:
name = event.text # Save the name wherever you want
await event.respond('Nice! What is your age?')
conversation_state[who] = State.WAIT_AGE
elif state == State.WAIT_AGE:
age = event.text # Save the age wherever you want
await event.respond('Thank you!')
# Conversation is done so we can forget the state of this user
del conversation_state[who]
# ...code to keep Telethon running...
通过这种方法,您可以随心所欲.您可以制作自己的装饰器并 return new_state
自动更改状态或仅在状态正确时才进入处理程序,您可以保持状态不变以创建循环(如果用户输入无效例如年龄数字),或执行任何跳转到您想要的其他状态.
You can get as fancy as you want with this approach. You could make your own decorators and return new_state
to automatically change the state or to only enter a handler if the state is correct, you can leave the state unchanged to create a loop (if the user entered an invalid age number for example), or perform any jumps to other states you want.
这种方法非常灵活和强大,尽管可能需要一些时间来适应它.它还有其他好处,比如很容易只保留您需要的数据,无论您想要什么.
This approach is very flexible and powerful, although it may need some time to get used to it. It has other benefits like being very easy to persist only the data you need, however you want.
我不建议使用 Telethon 1.0 的 client.conversation
,因为您很快就会遇到限制.
I would not recommend using Telethon 1.0's client.conversation
because you will quickly run into limitations.
这篇关于如何将 ConversationHandler 模块从 Python-Telegram-Bot 迁移到 Telethon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!