简体   繁体   English

Python 3.X 和电报机器人 5.6 消息转发不起作用

[英]Python 3.X and telegram-bot 5.6 message forwarding is not working

Scenario: I am writing a bot to transfer couple of selected messages from a private channel to the person(which I have his/her I) or to another group that I have its id场景:我正在编写一个机器人来将几个选定的消息从私人频道传输给这个人(我有他/她的我)或我有它的 id 的另一个组

Status:地位:

  • Bot: Admin of my private channel.(from part or source): (id=-15XXXXXX) Bot:我的私人频道的管理员。(来自部分或来源):(id=-15XXXXXX)

  • Bot: Privacy mode is off机器人:隐私模式已关闭

  • Bot: normal member of forwarding to group (to part or sink) (I have the id as well) Bot:转发到组的普通成员(到部分或接收器)(我也有 id)

  • me: Admin of private channel我:私人频道的管理员

  • me: normal member我:普通会员

  • user: I have his/her telegram id (I have the id as well)用户:我有他/她的电报 ID(我也有这个 ID)

Following is the code of forward function以下是转发 function 的代码

    def forwardMsg(self, update: Update, context: CallbackContext):
            context.bot.forwardMessage(chat_id=update.effective_chat.id, 
            from_chat_id=-15XXXXXX, message_id=4463)

Following is my main code以下是我的主要代码

    forwardMsg_handler = CommandHandler('forwardMsg', forwardMsg)
    dispatcher.add_handler(forwardMsg_handler)

All other commands are working.所有其他命令都在工作。

I get following error in Pycharm console我在 Pycharm 控制台中收到以下错误

telegram.error.BadRequest: Chat not found

I searched everywhere, It must work but it doesn't.我到处搜索,它必须工作,但它没有。 WHY?为什么?

2022-01-11 19:24:05,822 - apscheduler.scheduler - INFO - Scheduler started
2022-01-11 19:52:32,661 - telegram.ext.dispatcher - ERROR - No error handlers are 
registered, logging exception.
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site- 
packages\telegram\ext\dispatcher.py", line 555, in process_update
handler.handle_update(update, self, check, context)
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site- 
packages\telegram\ext\handler.py", line 198, in handle_update
return self.callback(update, context)
File "C:\Users\a\PycharmProjects\telegramBot\commands\bot_commands.py", line 70, in 
forwardMsg
context.bot.forwardMessage(chat_id=update.effective_chat.id, from_chat_id=-154*****, 
message_id=4463)
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site-packages\telegram\bot.py", 
line 130, in decorator
result = func(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site-packages\telegram\bot.py", 
line 641, in forward_message
protect_content=protect_content,
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site- 
packages\telegram\ext\extbot.py", line 209, in _message
protect_content=protect_content,
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site-packages\telegram\bot.py", 
line 336, in _message
result = self._post(endpoint, data, timeout=timeout, api_kwargs=api_kwargs)
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site-packages\telegram\bot.py", 
line 296, in _post
f'{self.base_url}/{endpoint}', data=data, timeout=effective_timeout
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site- 
packages\telegram\utils\request.py", line 366, in post
**urlopen_kwargs,
File "C:\ProgramData\Anaconda3\envs\pythonProject\lib\site- 
packages\telegram\utils\request.py", line 279, in _request_wrapper
raise BadRequest(message)

telegram.error.BadRequest: Chat not found

Question is also asked in telegram-bot issues here在电报机器人问题中也提出了问题here

As already answered on this GitHub thread , you should double check the channel ID as channel IDs start with a -100.正如已在此 GitHub 线程上回答的那样,您应该仔细检查通道 ID,因为通道 ID 以 -100 开头。

Please wait for a reasonable amount of time before you ask the same question in a different place and always indicate on both threads that you have asked the question also elsewhere.请等待一段合理的时间,然后再在不同的地方提出相同的问题,并始终在两个线程上表明您也在其他地方提出过该问题。

Base on CallmeStag input, I couldnt find any -100 at the beginning of基于 CallmeStag 输入,我在开头找不到任何 -100

  • Channel link频道链接
  • Copy Post link parts.复制帖子链接部分。 However there was a gist on github which was mentioning that we have to add -100 as prefix to the channel ID.然而, 在 github 上有一个要点,其中提到我们必须在频道 ID 中添加 -100 作为前缀。

Therefore the solution is -100 + channel ID taken from Copy Post Link as an integer for from_chat_id因此,解决方案是 -100 + 从复制帖子链接中获取的频道 ID,作为 from_chat_id 的 integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM