Skip to content

telegram bot does not respond to inlinebuttons #20

@rghWoagh

Description

@rghWoagh

the bot responds as expected to /start, but nothing responds to button clicks in the message

`from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackContext, CallbackQueryHandler, Application
import telegram.ext
import random

current_number = 0
game_active = False

async def start(update: Update, context: CallbackContext) -> None:
global current_number
global game_active

current_number = random.randint(1, 100)
game_active = True

keyboard = [
    [InlineKeyboardButton("Больше", callback_data='more')],
     [InlineKeyboardButton("Меньше", callback_data='less')]
]

reply_markup = InlineKeyboardMarkup(keyboard)

await update.message.reply_text(f"Загадано число {current_number} Больше или меньше?", reply_markup=reply_markup)

async def check_guess(update: Update, context: CallbackContext) -> None:
global current_number
global game_active

query = update.callback_query
choice = query.data

new_number = random.randint(1, 100)

if (choice == 'more' and new_number > current_number) or (choice == 'less' and new_number < current_number):
    message = "Вы выиграли! Вот следующее число: {}".format(new_number)
else:
    message = "Вы проиграли. Попробуйте снова. Вот следующее число: {}".format(new_number)

current_number = new_number
game_active = False

await query.edit_message_text(text=message)

def main() -> None:
application = Application.builder().token('token').build()
application.add_handler(CommandHandler('start', start))
application.run_polling()

if name == 'main':
import asyncio
asyncio.run(main())`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions