通过电报机器人处理和应答消息

时间:2018-10-19 19:16:43

标签: python python-telegram-bot

我正在尝试制作一个可与用户玩简单的猜数字游戏的python电报机器人。

#!/usr/bin/env python3
#!coding: utf-8

"""
sudo apt install python3-pip
pip3 install python-telegram-bot
"""

from random import randint

import traceback

from telegram.ext import Updater, CommandHandler
bot_token = "Your_token_here"

以下是应该读取用户的guess并将其与实际answer进行比较的主要功能。如果这两个相等,则应返回"You're right""Unfortunately you're wrong otherwise"

但是,它仅返回第二个选项。此外,input方法在终端中起作用,而不是在机器人的对话框界面中起作用。如何修改下面的功能来解决这些问题?

def bot_game(bot, update):
    answer = randint(1, 1)
    msg = "I made up a number from 1 to 3. Try to guess it."
    update.message.reply_text(msg)

    guess = input("Type your guess here: ")

    if number == guess:
        msg = "You're right!"
    else:
        msg = "Unfortunatlly, you're wrong."
    update.message.reply_text(msg)


if __name__ == "__main__":

    updater = Updater(bot_token)

    updater.dispatcher.add_handler(CommandHandler('game', bot_game))

    updater.start_polling()

    updater.idle()

0 个答案:

没有答案