Hello, today we are going to learn how we can automatically post messages using a Telegram bot using Python.
*Remember that the name must contain the word “bot” for BotFather to accept it.
Once you have added the name, it will return the access token that you should keep track of.
Copy the token to have it handy, and let’s create a new Python file.
First, we need to install the pyTelegramBotAPI library.
pip install pyTelegramBotAPI
Now let’s create our file, “mi_bot.py”.
And add the following code:
import telebot
TOKEN_TELEGRAM = "token_copied"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
This initializes the token.
Now we need to add our bot to a group or channel. To do this, go to the group or channel and search for it to add it, and also
Once you have added the name, it will return the access token that you should keep track of.
Copy the token to have it handy, and let’s create a new Python file.
First, we need to install the pyTelegramBotAPI library.
pip install pyTelegramBotAPI
Now let’s create our file, “mi_bot.py”.
And add the following code:
import telebot
TOKEN_TELEGRAM = "token_copied"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
This initializes the token.
Now we need to add our bot to a group or channel. To do this, go to the group or channel and search for it to add it, and also give it administrator permissions.
Go to channel settings > administrators:
And choose “Add administrators”:
And add the bot with all permissions:
Now we need to obtain the chat id. To do this, simply get the link of the group or channel. In my case:
https://t.me/quiero_libros_com
And get the part at the end and add “@” to it:
@quiero_libros_com
To publish, add the following code:
CHAT_ID = "@quiero_libros_com "
my_bot.send_message(CHAT_ID, message)
The final code looks like this:
import telebot
TOKEN_TELEGRAM = "token_copied"
my_bot = telebot.TeleBot(TOKEN_TELEGRAM )
CHAT_ID = "@quiero_libros_com "
my_bot.send_message(CHAT_ID, message)
And this is the result:
Now you can apply this to your chats or channels.
Post Views: 1
Related