Build Telegram Bot to send Daily Notification
Tags: telegram, api, bot, automation
Published: 2019-08-15 02:16:00

I'm running a website of a curated list of [Public API](https://public-apis.io) 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](https://nocodeapi.com/telegram-api)

### **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](https://res.cloudinary.com/mohddanish/image/upload/v1565777567/42B0E406-7169-4B30-832E-13F829D4ED89.jpg)

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](https://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](https://public-apis.xyz/chucknorris-io-7486)

```javascript
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!')
})
```

Setup Environment variable into **.env** file.

```javascript
BOT_TOKEN=<bot_access_token>
GROUP_ID=<group_id>
```

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.

```javascript
https://api.telegram.org/bot<bot_access_token>/getUpdates
```

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.

```javascript
//like this object
"chat":{
   "id":-369192376, // this is the group id
   "title":"xyz",
   "type":"group",
   "all_members_are_administrators":true
}
```

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.

```javascript
https://chuck-noris-fun.glitch.me/random-jokes
//https://<your glitch project name>/random-jokes
```

Yes, it's working.

![Bot is Working](https://thepracticaldev.s3.amazonaws.com/i/gcflcozk0axbfh1gjcut.png)

**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](https://www.easycron.com/?ref=131858) It's so simple and easy to use.

![Add cron job](https://thepracticaldev.s3.amazonaws.com/i/ikhp6n7997wwkrw75fan.png)

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](https://twitter.com/mddanishyusuf)
Website: [mohddanish.me](https://mohddanish.me)
Projects: [NoCodeAPI](https://nocodeapi.com)