2

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
  • which module are you using? Commented Dec 24, 2021 at 6:09
  • I'm using Telegram module. Commented Dec 24, 2021 at 6:13
  • Assuming you're using python-telegram-bot it seems you're using old formatting, see Commented Dec 24, 2021 at 6:41

3 Answers 3

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response but the same error pops up...
0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.