【问题标题】:How to send a Private Message by chat_id in Telegram by Bot in Python 3如何通过 Python 3 中的 Bot 在 Telegram 中通过 chat_id 发送私人消息
【发布时间】:2021-11-09 09:50:56
【问题描述】:

我必须制作一个机器人,它将在组中发布消息和 InlineKeyboardButton,组成员将响应这些 InlineKeyboardButton。因此,在回调查询中,我向他们显示了一条警报消息,

现在我想向每个响应者直接发送私人消息到他们的收件箱,而不是在组中。

from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext

def start(update: Update, context: CallbackContext) -> None:
    """Sends a message with three inline buttons attached."""
    keyboard = [
        [InlineKeyboardButton("opt_a", callback_data='A')],
        [InlineKeyboardButton("opt_b", callback_data='B')],
        [InlineKeyboardButton("opt_c", callback_data='C')],

    ]

    reply_markup = InlineKeyboardMarkup(keyboard, resize_keyboard=False)
    update.message.reply_text("Question: Please Select an Option", reply_markup=reply_markup)


def button(update: Update, context: CallbackContext) -> None:
    """Parses the CallbackQuery and show the alert."""
    query = update.callback_query
    query.answer(text=f"Thanks for responding", show_alert=True)

    #############################################################
    Chat_id = query.message.chat.id

我需要做的是在下面的代码行中

# Send_Private_Message(chat_id=Chat_id, message_text = "Text of the message will b here...")

现在我想在这里给回复的用户发送一条私人消息 要内联键盘,我在上面的变量“Chat_id”中得到了他的聊天 ID, 但无法找到通过他的“Chat_id”向该人发送私人消息的方法

updater = Updater("TOKEN")

updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(button))

# Start the Bot
updater.start_polling()

# Bot in Idle Position
updater.idle()

【问题讨论】:

    标签: python-3.x telegram telegram-bot message python-telegram-bot


    【解决方案1】:

    您可以使用Bot.send_message 方法。 bot 实例以context.bot 的形式提供。请注意,(update.)message.reply_text 只是send_message快捷方式 ;)


    免责声明:我目前是python-telegram-bot的维护者

    【讨论】:

      猜你喜欢
      • 2016-05-09
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 2019-11-18
      • 2016-12-05
      • 1970-01-01
      相关资源
      最近更新 更多