如何通过电报python api获取消息的消息id

2024-09-30 08:20:53 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我知道这是个新手问题,但我已经被困了一段时间了

我刚开始使用pythonTelegarmapi,但我不知道如何获取message_id的消息。我该怎么做

import os
import telebot

API_KEY =  "API_KEY"

# Getting the bot instance
bot = telebot.TeleBot(API_KEY)

# Forward mesasage
# From https://core.telegram.org5/bots/api#forwardmessage
@bot.message_handler(commands=['forward'])
def forward(message):
    bot.forward_message('@send_to','@from', 1, False)
                                           ^^^
#                                          Here is supposed to be the message_id but I don't know how to get that. 

"""

那么,如何使用Python Telegram Bot Api检索聊天室/频道中特定消息的ID呢


Tags: thetokeyimportapiid消息message
2条回答

您需要:

bot.forward_message(mydebugid, message.chat.id, message.message_id, False)

其中mydebugid是要转发到的ID


import os
import telebot

API_KEY = " "

# Getting the bot instance
bot = telebot.TeleBot(API_KEY)

mydebugid = 123456

bot.send_message(mydebugid, "Wake")

# Forward mesasage
@bot.message_handler(commands=['forward'])
def forward(message):
    bot.forward_message(mydebugid, message.chat.id, message.message_id, False)


bot.polling()

enter image description here

函数接收的是message参数message.message_id应该可以做到这一点,另请参见https://github.com/eternnoir/pyTelegramBotAPI#types。请注意,您可能应该传递message.chat.id,而不是'@from',因为IISC您的代码片段不能保证message将是由用户名'@from'标识的聊天中发送的消息

相关问题 更多 >

    热门问题