我最近开始学习如何使用 pyTelegramBotAPI 创建电报机器人。
我正在制作一个只能在内联模式下工作的机器人。
When calling a bot in a group via @, it shows the available commands, when a command is selected, the result corresponding to this command is displayed.
我做了一个生成 1 到 100 之间的随机数的命令。
但是我遇到了一个问题 - 当通过内联模式以不同方式调用命令时,结果(随机数)是相同的。
我怎样才能做到,当我的命令从机器人的内联模式调用时,总是会生成一个新的随机数?
我的代码:
import telebot
from telebot import types
from random import randint
TOKEN = "Токен бота"
bot = telebot.TeleBot(TOKEN)
def rand():
return randint(1, 100)
@bot.inline_handler(lambda query: query.query == '')
def default_query(inline_query):
try:
r4 = types.InlineQueryResultArticle(4, 'случайное число', types.InputTextMessageContent(f'{rand()}'), thumb_url = 'https://i.ibb.co/xF9nrXP/image.png')
bot.answer_inline_query(inline_query.id, [r4])
except Exception as ex:
print(ex)
if (__name__ == '__main__'):
while True:
try:
bot.polling(none_stop = True)
except Exception as ex:
print(ex)