我正在寻找一种在用户响应后删除内联键盘按钮的方法。在用户输入 /default 后,电报机器人会要求用户选择一个选项。我希望能够删除用户多次选择响应按钮的选项。下面是我的示例代码,我使用的是 python-telegram-bot 包。
list_default_options = ['a', 'b',
'c', 'd']
def default_options(update, context):
"""Generates responses for default options."""
button_list = []
for each in list_options:
button_list.append(InlineKeyboardButton(each, callback_data=each))
reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
context.bot.send_message(chat_id=update.message.chat_id,
text="Choose option",
reply_markup=reply_markup)
def build_menu(buttons, n_cols=1, header_buttons=None, footer_buttons=None):
"""
Returns a list of inline buttons used to generate inlinekeyboard responses
:param buttons: `List` of InlineKeyboardButton
:param n_cols: Number of columns (number of list of buttons)
:param header_buttons: First button value
:param footer_buttons: Last button value
:return: `List` …Run Code Online (Sandbox Code Playgroud) 逻辑如下:
我使用这些例子作为我的出发点:
我的代码是这样的:
from telegram import (
Bot,
Update,
InlineKeyboardMarkup,
InlineKeyboardButton,
)
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
CallbackContext,
CallbackQueryHandler,
ConversationHandler,
)
def startCommand(update: Update, context: CallbackContext):
keyboardMarkup = InlineKeyboardMarkup(
[[InlineKeyboardButton('Share File 1', callback_data='sharingFile1')]]
)
update.message.reply_text(f'Howdy, {update.effective_user.first_name}.\nThis is the Main Menu.',
reply_markup=keyboardMarkup)
def convGetGMailAddr(update: Update, context: CallbackContext):
update.message.reply_text('Waiting for your gmail address.\n\nSend /end and I\'ll stop waiting.')
return convEmailAddr
def convMismatch(update: Update, context: CallbackContext):
text = f"""Sorry, …Run Code Online (Sandbox Code Playgroud) 我是 python 的初学者,我正在尝试使用 python-telegram-bot 模块制作一个机器人。但我无法理解其中的更新程序、上下文和调度程序是什么。谁能告诉我它是什么?
我使用 python-telegram-bot 框架创建了一个电报机器人。我有一些电话号码,现在我想向电话号码发送消息。这些电话号码在 Telegram 中有一个帐户,这是我的代码:
from telegram.ext import Updater, MessageHandler, Filters
def start_method(bot, update):
bot.send_message(chat_id='Phone number', "Hello")
def main():
updater = Updater(token='TOKEN')
updater.dispatcher.add_handler(MessageHandler(Filters.text, start_method))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
但是;这个代码不起作用并且不会给我一个错误!
我能怎么做???
我正在使用此ConversationHandler脚本作为程序的基础。
如何从以前的状态中检索用户答案?例如,当询问用户有关其生物的信息时,我该如何打印其性别(这是首先询问的内容)?
看起来每个函数都返回下一步(GENDER-> PHOTO-> LOCATION-> BIO),但是有没有办法查看以前的输入是什么?
当我按下带有InlineKeyboardButton. 我正在尝试以下但没有运气:
bot.send_message(chat_id=chat_id,
text='/help',
parse_mode=telegram.ParseMode.HTML)
Run Code Online (Sandbox Code Playgroud) 给出下面的代码:
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
Run Code Online (Sandbox Code Playgroud)
是否可以定期调用此函数并使我的机器人自动将消息发送给用户,而不是用户键入“/start”
是否可以删除我与机器人聊天的所有聊天记录(消息)。
所以控制台版本是这样的:
import os
os.sys("clear") - if Linux
os.sys("cls") - if Windows
Run Code Online (Sandbox Code Playgroud)
我想要的只是使用 bot 删除聊天中的所有消息。
def deleteChat(message):
#delete chat code
Run Code Online (Sandbox Code Playgroud) 我正在构建一个基于 Django 的项目,我的目的之一是拥有一个从 Telegram 组接收信息的 Telegram 机器人。我能够实现机器人在 Telegram 中发送消息,没有任何问题。
目前,我有几个与 Beat 一起运行的 Celery 任务以及 Django Web(已解耦)。这里一切都好。
我已经看到 python-telegram-bot 正在一个示例中运行一个函数(https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py)它正在等待空闲以接收来自 Telegram 的数据。现在,我在 Celery 中的所有任务此时都是周期性的,并且由 Beat 每 10 或 60 分钟调用一次。如何在我的配置中使用 Celery 运行这个非周期性任务?我说非周期性是因为我知道它将等待内容直到被手动中断。
姜戈~=3.2.6
芹菜~=5.1.2
CELERY_BEAT_SCHEDULE = {
'task_1': {
'task': 'apps.envc.tasks.Fetch1',
'schedule': 600.0,
},
'task_2': {
'task': 'apps.envc.tasks.Fetch2',
'schedule': crontab(minute='*/60'),
},
'task_3': {
'task': 'apps.envc.tasks.Analyze',
'schedule': 600,
},
Run Code Online (Sandbox Code Playgroud)
}
在我的tasks.py 中,我有这样的任务之一:
@celery_app.task(name='apps.envc.tasks.TelegramBot')
def TelegramBot():
status = start_bot()
return status
Run Code Online (Sandbox Code Playgroud)
作为 start_bot 实现,我只是复制了 echobot.py 示例,并在其中添加了我的 TOKEN(当然示例中不同命令的函数也在那里)。
python ×8
telegram ×8
telegram-bot ×3
python-3.x ×2
bots ×1
button ×1
celery ×1
django ×1
python-2.7 ×1