Home > Software engineering >  Python 3.X and telegram-bot 5.6 message forwarding is not working
Python 3.X and telegram-bot 5.6 message forwarding is not working

Time:01-13

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

Status:

  • Bot: Admin of my private channel.(from part or source): (id=-15XXXXXX)

  • Bot: Privacy mode is off

  • Bot: normal member of forwarding to group (to part or sink) (I have the id as well)

  • me: Admin of private channel

  • me: normal member

  • user: I have his/her telegram id (I have the id as well)

Following is the code of forward 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

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

CodePudding user response:

As already answered on this GitHub thread, you should double check the channel ID as channel IDs start with a -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.

CodePudding user response:

Base on CallmeStag input, I couldnt find any -100 at the beginning of

  • 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.

Therefore the solution is -100 channel ID taken from Copy Post Link as an integer for from_chat_id

  •  Tags:  
  • Related