skip to Main Content

So i have this python code that figures out channel chat id to send message for multiple channels in a given list.

I only have the channel invite link

I still didn’t figure out how can i get the channel id by the name only.

import os
import telebot
API_KEY = os.environ['API_KEY']
bot = telebot.TeleBot(API_KEY)
a=input("edit msg.txt to send the msg if done press enter")
message=open("message.txt","a+")
bot.send_message(chat_id,message)

2

Answers


  1. You can use the getChat method to resolve a channel username to the chat id. You can also just send the message using the username – see the docs of sendMessage and e.g. this answer.

    Login or Signup to reply.
  2. If you prefer to use Telegram API together with javascript and axios library then you might consider the following:

    const method = 'get'
    
    const headers: any = {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
      timestamp: +new Date(),
    }
    
    const options = { headers: { ...headers } }
    
    const urlTelegramBase =
      'https://api.telegram.org/bot123456:ABCDEF'
    
    const urlGetUpdates = `${urlTelegramBase}/getUpdates`
    const username = 'user_name'
    
    const {
      data: { result: messages },
    } = await axios[method](urlGetUpdates, options)
    
    const chat_id = messages.find(
      messageBlock => messageBlock.message.chat.username === username
    ).message.chat.id
    
    console.info('chat_id': chat_id)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search