在python中处理Telegram机器人的多个问题 [英] Handle multiple questions for Telegram bot in python

查看:523
本文介绍了在python中处理Telegram机器人的多个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Telegram bot API在Python中编程一个电报bot.我面临着管理需要用户回答的问题.当程序正在等待一个用户的回答而另一个用户在第一个用户做出响应之前 请求信息或询问另一个问题时,就会出现问题.

I'm programming a telegram bot in Python using the Telegram bot API. I'm facing the problem of managing questions that need an answer of the user. The problem arises when the program is waiting for an answer of one user and another user request information or ask another question before the first user responds.

Telegram API使用代码来处理请求.当您要求更新时,您将包含一个代码.如果您发送的代码高于请求代码,则将其标记为已处理,并以电报方式将其删除,并且不再显示在更新中.该代码是顺序的,因此,如果将更新3标记为已处理,则更新1和2也将被删除.

The Telegram API uses a code to handle the request. When you ask for updates you include a code. If the code you send is higher than a request code, it is mark as handled and telegram delete it and no longer appears in the updates. This code is sequential, so if you mark update 3 as handled, updates 1 and 2 are erased as well.

问题是为什么最好的植物/优雅方式来处理需要用户回答的多个请求?

推荐答案

目前尚无最Python的方法.这是您必须编程要解决的问题.

There is not a most pythonic way of doing this. It is a problem you have to program to solve.

基本上,您必须维护一些有关每个用户的状态变量.当收到新消息时,该漫游器会检查用户所处的状态,并做出相应的响应.

Basically, you have to maintain some state variables concerning each user. When a new message arrives, the bot checks what state that user is in, and responds accordingly.

假设您有一个函数handle(msg),该函数将为每条到达的消息调用:

Suppose you have a function, handle(msg), that gets called for each arriving message:

user_states = {}

def handle(msg):
    chat_id = msg['chat']['id']

    if chat_id not in user_states:
        user_states[chat_id] = some initial state ...

    state = user_states[chat_id]

    # respond according to `state`

这将用于一个简单的程序.

This will do for a simple program.

对于更复杂的情况,我建议使用 telepot ,这是我为之创建的Python框架电报Bot API .它具有专门解决此类问题的功能.

For more complicated situations, I recommend using telepot, a Python framework I have created for Telegram Bot API. It has features that specifically solve this kind of problems.

例如,下面是一个机器人,它计算单个用户发送了多少条消息.如果10秒钟后未收到任何消息,它将重新开始(超时).每次聊天都进行计数-这很重要.

For example, below is a bot that counts how many messages have been sent by an individual user. If no message is received after 10 seconds, it starts over (timeout). The counting is done per chat - that's the important point.

import sys
import telepot
from telepot.delegate import per_chat_id, create_open

class MessageCounter(telepot.helper.ChatHandler):
    def __init__(self, seed_tuple, timeout):
        super(MessageCounter, self).__init__(seed_tuple, timeout)
        self._count = 0

    def on_message(self, msg):
        self._count += 1
        self.sender.sendMessage(self._count)

TOKEN = sys.argv[1]  # get token from command-line

bot = telepot.DelegatorBot(TOKEN, [
    (per_chat_id(), create_open(MessageCounter, timeout=10)),
])
bot.notifyOnMessage(run_forever=True)

通过以下方式运行程序:

Run the program by:

python messagecounter.py <token>

转到项目页面,以了解更多信息.有很多文档和不平凡的例子.

Go to the project page to learn more if you are interested. There are a lot of documentations and non-trivial examples.

这篇关于在python中处理Telegram机器人的多个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆