我想使用 python-telegram-bot 参考此页面在 Python 脚本中打印用户信息
但是当我输入
print update.message.from
关键字.from被识别为内部保留关键字,我收到无效的语法错误。
我该如何解决?
谢谢。
我正在通过使用制作电报机器人python-telegram-bot并将其添加到组中。
我想指定一个列表,如果列表中的句子发送到组中,则电报的机器人将从组中删除该消息。我应该使用哪些模块和代码来做到这一点?
请给我一个完整的描述。我是新来的
我最近注册了使用电报机器人 API,但是当我查看每个示例时,它说要包含我们自己的 API TOKEN,但没有提到如何获取令牌。
当我访问 Telegram 站点时,它向我显示的只是一些 api id、hash 和一个 RSA 密钥对。
如何生成电报 API令牌或如何以及从何处获取?
这是我的一段代码
bot.edit_message_text(chat_id = CHAT_ID, message_id = MESSAGE_ID, text = "message has been updated", reply_markup=inline_keyboard)
Run Code Online (Sandbox Code Playgroud) 我现在正在为我的电报开发一个 python 脚本。问题是:
我如何知道我的机器人何时被添加到组中?有没有事件或其他什么东西?我希望 Bot 向他添加的群组发送消息,向其打招呼以及他可以使用的功能。
我不知道是否有任何类型的处理程序能够处理这个问题。
我正在制作一个电报机器人,它会询问用户不同的问题,我需要将他们的答案保存到变量中。
@bot.message_handler(commands=['addproduct'])
def handle_text(message):
bot.send_message(message.chat.id, "Price =")
Run Code Online (Sandbox Code Playgroud)
然后用户回答,程序应该将答案保存到可变价格中。
如何将用户的消息保存到变量中?
我正在尝试调整KeyboardButton我的电报机器人的大小。我正在为我的机器人使用Python-Telegram-Bot包装器。我找到了button_row选项(docs),但我不明白应该将其粘贴到哪里。
代码:
def start(update, context):
custom_keyboard = [['top-left', 'top-right'],
['bottom-left', 'bottom-right']]
reply_markup = ReplyKeyboardMarkup(custom_keyboard)
update.message.reply_text(text="Custom Keyboard Test", reply_markup=reply_markup)
Run Code Online (Sandbox Code Playgroud) 我一直在制作一个机器人,我用于filters=Filters.privatestart_command 然后我在 a 中使用它MessageHandler,它是代码: dp.add_handler(CommandHandler('start', start_command, filters=Filters.private)) dp.add_handler(MessageHandler(Filters) .private, start_keyboard_answers))
它工作得很好,但问题是我需要在第二个过滤器内制作另一个过滤器,代码应该是这样的:
dp.add_handler(CommandHandler('start', start_command, filters=Filters.private)) dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers, filters=Filters.private)) dp.add_handler(MessageHandler(Filters.private, start_keyboard_answers) )
但这是不可能的,我在第二行有两个过滤器,我该如何处理?如果你想知道我在做什么,我会把整个项目放在这里(我在第二个键盘之后寻找其他按钮,就像我对第一个和第二个键盘所做的一样):
from telegram.ext import Updater, Filters, CommandHandler, MessageHandler
from telegram import ReplyKeyboardMarkup
BOT_TOKEN = ''
def start_command(update, context):
chat_id = update.message.chat.id
context.bot.send_message(
chat_id=chat_id,
text='Hey, welcome to Chocolate Coffee!'
)
keyboard = [
['Make an Order']
]
context.bot.send_message(
chat_id=chat_id,
text='How can I help you?',
reply_markup=ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True)
)
def start_keyboard_answers(update, context):
message_text = str(update.message.text)
if message_text.lower() == …Run Code Online (Sandbox Code Playgroud) 当我打印一个对象时:
print(permissions)
Run Code Online (Sandbox Code Playgroud)
它的输出如下:
{'can_send_messages': True, 'can_send_media_messages': True, 'can_send_polls': True, 'can_send_other_messages': True, 'can_add_web_page_previews': True, 'can_change_info': True, 'can_invite_users': True, 'can_pin_messages': True}
Run Code Online (Sandbox Code Playgroud)
与映射对象完全相同,但是当我尝试将其作为参数时,例如:
some_method(id, **permissions)
Run Code Online (Sandbox Code Playgroud)
它给出错误:
argument after ** must be a mapping, not ChatPermissions
Run Code Online (Sandbox Code Playgroud)
有什么问题以及如何解决它?
我被困在我的代码中,因为我不知道如何输入/导出我的机器人转发的传出消息的 message_id。
背景:这只是我的代码的一部分,我随后会将其集成到主代码中。这里,我正在测试转发消息+删除消息的功能。我能够成功地将它们转发出去,但我坚持删除它们。我可以提供 chat_id 的输入,但无法删除要删除的 message_id。有没有办法做到这一点,特别是当我要集成到我的主脚本中时,它可以有几个组来管理。请帮助我这个菜鸟。谢谢你!
我的脚本:
import logging
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
)
TOKEN = "PUT TOKEN HERE" #INPUT HERE
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
MSG, DELETE_MSG = range(2)
def start(update: Update, context: CallbackContext) -> int:
update.message.reply_text(
'Hi! Please post the message you would like to share:')
return MSG
def send(update: Update, context: CallbackContext) -> int:
user …Run Code Online (Sandbox Code Playgroud)