Telegram Bot API Python - telegram.error.BadRequest: Chat not found

Viewed 607

I want to create a bot to post my texts to a channel... Can anyone help me with the code?

this is the code that I tried :

import telegram

token = "5002307835:AAGOu4f******************"
chat_id = "1382******"


bot = telegram.Bot(token)


def send_message(message):
    return bot.send_message(chat_id,message)


send_message("HI")

but i got this error : telegram.error.BadRequest: Chat not found

also i tried : chat_id = "-1382******" and chat_id = -1382****** and chat_id = 1382******

3 Answers

use it without " "

e.g.

import telegram

chat_id = 1382******
token = "TOKEN"

if not work, try it with - and no "", it will works!

e.g.

import telegram
    
chat_id = -1382******
 token = "TOKEN"

Channels ids usually starts with -100, so

chat_id = -1001382******

Put it as integer

You can also get the channel id sending a message in the channel as normal user and handling it with the bot. In message.chat.id you will see the correct id Or just forward a channel message to the bot https://t.me/nguLikJSONbot

First of all, you should be administrator of a channel and then send message to it. For send text, file or photo with caption to a specific user, the target user should be member of your bot and then change chat_id to user id in telegram.

import telegram

token = "5002307835:AAGOu4f******************"

# "@{0}".format("botfather") ==> @botfather
chat_id = "@{0}".format("your_channel_name")
# For sending message to a specific user
# chat_id = 18558...    

bot = telegram.Bot(token)


def send_message(message):
    return bot.send_message(chat_id, message)

your_msg = "Hello"
send_message(your_msg)
Related