Share how to use Telegram as automatic message receiving function through the Telegram Bot API

Tram Ho

Hello everyone, here is Quang again

Today, I come up here again and share with you about how I use Telegram to receive weather forecast messages through the Telegram Bot API.

And don’t let you wait long, I will start right away!

First, you need to have a telegram account, the registration is quite simple, because almost telegram is already quite popular, especially for devs, coin players … the closer the telegram, In addition to the classrooms sharing study materials at night, there are many interesting things on telegram for you to learn, just saying that, you understand, you will not take the effort to tell me a lot of things.

Putting aside the rambling problems above, once you have an account on telegram, the next thing, we need to create a channel on our telegram account, which will be used to use the message receiving function when the tele bot sends the command

The way to create a channel is quite simple, you open up telegram, here I do it on telegram desktop version (personal computer), on mobile is the same.

And after clicking, you name your channel, upload an avatar, add a description (if necessary) that’s done, we have created a successful channel.

Next, we need to create a bot on telegram, create a bot on telegram, then after you log into Telegram in the search box, find BotFather, then type / newbot to create,

Once created, you just need to get the bot’s API Token to use.

Continuing, we will go to the api documentation to see usage, the api documentation:

Here, I used the function to send messages with pictures, so I came to this api:

See what parameters to use? I send weather information so I only need to use 3 parameters chat_idphotocaption. The chat_id parameter is the group id of the group chat, or the username of the channel, the channel I created at the name will be in the form @ten_channel then this parameter accepts numeric or string type. So to get the username of the channel, what type? Very simply, you click on the name of the channel, you will see information, at the Channel Info window, you will get the username at the link. For example: => @codedaokysu (this is the username)

Once you have the information, you need to write code to send messages, combine with the Bot Token when creating a bot to use to send messages to the channel. You see my reference code below.

Once setup is complete, you run the code, it will send as shown below

And to automatically send messages, you need to use the cronjob function of hosting, here I set up sending messages at 7:00 am every day, every day.

That’s it, thank you for reading the article, if you have any questions, please leave a comment below to exchange.

Share the news now