[英]Telegram Quiz Bot with pyTelegramBotAPI
Trying to build a Telegram Quiz Bot using pyTelegramBotAPI.尝试使用 pyTelegramBotAPI 构建 Telegram Quiz Bot。 I'm using sched to schedule the message Handler but i don't know how to stop the Message Handler and return to my Main Script which will scheudle the next Round.
我正在使用 sched 来安排消息处理程序,但我不知道如何停止消息处理程序并返回到我的主脚本,它将安排下一轮。
Tryed to use timeout but it is not working!尝试使用超时,但它不起作用!
My Code:我的代码:
import telebot
import sched, time
def listen():
print("Send my your Answer")
@bot.message_handler(func=lambda message: True, content_types=['text'])
def command_default(m):
print(m.text)
bot.polling()
API_TOKEN = 'xxxx'
s = sched.scheduler(time.time, time.sleep)
bot = telebot.TeleBot(API_TOKEN)
s.enter(50, 1, listen)
s.run()
In this use case you have to use something called a Finite State Machine (FSM) .在此用例中,您必须使用称为有限状态机 (FSM)的东西。 You keep track of user states, such as one where the user is ready to send an answer.
您跟踪用户状态,例如用户准备发送答复的状态。
This is already implemented in pyTelegramBotAPI, with the next_step_handler() .这已经在 pyTelegramBotAPI 中通过next_step_handler()实现。 However, I suggest you instead create your own solution, as the one provided by the wrapper is quite buggy.
但是,我建议您改为创建自己的解决方案,因为包装器提供的解决方案存在很多问题。
Here is an example (you can translate the page): https://groosha.gitbooks.io/telegram-bot-lessons/content/chapter11.html这是一个示例(您可以翻译页面): https ://groosha.gitbooks.io/telegram-bot-lessons/content/chapter11.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.