Skip to main content
 首页 » 编程设计

python之电报中的机器人操作后如何处理用户消息

2024年11月01日16kenshinobiy

我使用 python-telegram-bot 库用 python 编写了一个简单的电报机器人,它可以掷一个 n 面骰子并将结果返回给用户。

我的代码:

# Let's make a dice bot 
import random 
import time 
from telegram.ext import Updater,CommandHandler,MessageHandler, Filters 
from telegram import Update 
 
updater = Updater(token='MY-TOKEN', use_context=True) 
dispatcher = updater.dispatcher 
 
# relevant function here: 
def dice_roll(sides): 
    roll = random.randint(1,sides) 
    return roll 
 
def dice(update, context): 
    result = dice_roll(int(context.args[0])) 
    lucky_dice = "You rolled a "+str(result) 
    context.bot.send_message(chat_id=update.message.chat_id, text=lucky_dice) 
dice_handler = CommandHandler('rollDice', dice) 
dispatcher.add_handler(dice_handler) 
 
# Polling function here: 
updater.start_polling(clean=True) 

我想改进什么

我不太喜欢目前用户与机器人交互的方式。要掷骰子,用户必须在命令参数中定义骰子的面数,如下所示:

$user: /rollDice 6 
$bot: You rolled a 5 

这不是真正的用户友好,如果用户向参数添加任何其他内容或只是忘记,它也容易出错。

相反,我希望机器人明确要求用户输入,如下所示:

$user: /rollDice 
$bot: Please enter the number of sides, the die should have 
$user: 6 
$bot: You rolled a 5 

我查看了 force_reply,但我的主要问题是我不知道如何在处理函数中访问新的更新/消息。

如果我尝试这样的事情:

def dice(update, context): 
    context.bot.send_message(chat_id=update.message.chat_id, text="Please enter the number of sides, the die should have") # new line 
    result = dice_roll(int(context.args[0])) 
    lucky_dice = "You rolled a "+str(result) 
    context.bot.send_message(chat_id=update.message.chat_id, text=lucky_dice) 
dice_handler = CommandHandler('rollDice', dice) 
dispatcher.add_handler(dice_handler) 

它并没有真正起作用,因为 a) 机器人并没有真正给用户时间来回复(在这里添加一个 sleep 语句似乎是非常错误的)和 b)它指回原来的“命令消息”,而不是同时发送任何新消息。

感谢任何帮助。

请您参考如下方法:

您可以使用 ConversationHandler 来实现这一点,我正在为我的机器人使用类似的代码,它会询问用户公交车号码并回复时间表。大部分代码借自:https://codeclimate.com/github/leandrotoledo/python-telegram-bot/examples/conversationbot.py/source

from telegram.ext import (Updater, CommandHandler, RegexHandler, ConversationHandler) 
import random 
 
# states 
ROLLDICE = range(1) 
 
def start(bot, update): 
    update.message.reply_text('Please enter the number of sides, the die should have') 
 
    return ROLLDICE 
 
def cancel(bot, update): 
    update.message.reply_text('Bye! I hope we can talk again some day.') 
    return ConversationHandler.END 
 
def rolldice(bot, update): 
    roll = random.randint(1, int(update.message.text)) 
    update.message.reply_text('You rolled: ' + str(roll)) 
    return ConversationHandler.END 
 
def main(): 
    updater = Updater(TOKEN) 
    dp = updater.dispatcher 
 
    conv_handler = ConversationHandler( 
        entry_points=[CommandHandler('start', start)], 
        states={ROLLDICE: [RegexHandler('^[0-9]+$', rolldice)]}, 
        fallbacks=[CommandHandler('cancel', cancel)] 
    ) 
 
    dp.add_handler(conv_handler) 
    updater.start_polling() 
 
    updater.idle() 
 
if __name__ == '__main__': 
    main() 

输出:

/start 
Please enter the number of sides, the die should have 
6 
You rolled: 2