DEV Community

Mohd Danish
Mohd Danish

Posted on • Edited on

Build Telegram Bot to send Daily Notification

I'm running a website of a curated list of Public API for the Front-end Developers and There is a telegram group community for the users. So, I decide to keep feed one Random API every day into the channel. Then, I decided to automate this task rather than manual share with the group.

Using NoCode tool

If you don't want to code & setup a server then you can set up telegram API within 5 minutes. Telegram API with NoCode

Using Code

Let's start coding now.

Step 1: Setup new Bot with a Bot(@botfather)

Search for the name "@botfather" in the telegram search and add tap on this to send command.

/newbot to make a bot and follow the instructions. Finally, you will get access_token to use Telegram API.

Bot Tocken

So, now you have created a bot. Now search it on telegram search(@name_bot).

Now let's code this bot to do automate the tasks. So, We are automating a daily news feed about random API into a telegram channel.

Step 2: Let's Setup a Server for this bot

I'm using the glitch.com to build server and using NodeJS to code the bot.

Make an Express project in Glitch. You can make this project public and also private too.

Using a Public APIs of Chuck Norris to send daily fun quotes of the chuck. Here I found that API Chuck Norris API

var express = require('express')
var app = express()
var bodyParser = require('body-parser')
const axios = require('axios')
const {
    Telegram
} = require('telegraf')

const tg = new Telegram(process.env.BOT_TOKEN)

app.use(bodyParser.json()) // for parsing application/json

app.use(
    bodyParser.urlencoded({
        extended: true
    })
)

app.get('/random-jokes', function(req, res) {
    axios.get('https://api.chucknorris.io/jokes/random').then(res => {
        const txt = res.data.value
        tg.sendMessage(process.env.GROUP_ID, txt)
    })
    res.send('Joke is delivered')
})

// Finally, start our server
app.listen(3000, function() {
    console.log('Telegram app listening on port 3000!')
})
Enter fullscreen mode Exit fullscreen mode

Setup Environment variable into .env file.

BOT_TOKEN=<bot_access_token>
GROUP_ID=<group_id>
Enter fullscreen mode Exit fullscreen mode

Now get these environment variables. You can easily get Access Token with @botfather.

To get group id first add bot into the group where you want to send the daily notification. After adding the bot call below API into the browser to get the Group ID.

https://api.telegram.org/bot<bot_access_token>/getUpdates
Enter fullscreen mode Exit fullscreen mode

So, this API will return a JSON Array of objects. Find one object that has a group name and that object id is the group id.

//like this object
"chat":{
   "id":-369192376, // this is the group id
   "title":"xyz",
   "type":"group",
   "all_members_are_administrators":true
}
Enter fullscreen mode Exit fullscreen mode

That's all. Now you have access_token and group id. Your server is ready to send the notification.

Now try to run the API URL into the browser and see.

https://chuck-noris-fun.glitch.me/random-jokes
//https://<your glitch project name>/random-jokes
Enter fullscreen mode Exit fullscreen mode

Yes, it's working.

Bot is Working

Step 3: Use CRON job to call that API endpoint automatically. So, there are many only tools to run a CRON job. I'm using called Easy Cron It's so simple and easy to use.

Add cron job

Yes!!! You made your telegram bot. There are more to explore. Comment down below to let me know that you need more advanced telegram bot tutorial.

Thanks For Reading.

Twitter: @mddanishyusuf
Website: mohddanish.me
Projects: NoCodeAPI

Top comments (6)

Collapse
 
jefernisee profile image

hello!! i used the glitch to do the coding but they say there’s a error and i tried debugger it doesn’t work. i also set up a group in telegram but still can’t get the group id too. isit ok if u can help me?? :))

Collapse
 
mddanishyusuf profile image
Mohd Danish • Edited

Hello. First, send any message in the group and then try to access the this API https://api.telegram.org/bot<bot_access_token>/getUpdates

Or there are many bots for the telegram to get group id try them also.

Collapse
 
braiannunes profile image
Braian Nunes

Did you install all packages/dependencies?

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
keither profile image
Keither

I did it, but my notification delay too slow (15-30 minutes), sometimes not at all until I open the app. I checked my phone all setting ok, i don't know why, sorry my englisht not good!

Collapse
 
epsi profile image
E.R. Nurwijayadi

My.. yet another bash bot article in telegram.

epsi-rns.gitlab.io/code/2018/01/20...