Make a Telegram Bot for free with MongoDB Atlas. Part 2

In the first part of this post we saw how to implement a simple Telegram bot that answers with phrases, picked randomically among a defined set of phrases. Read it if you want to know how to create the bot on Telegram and how to implement it using MongoDB Atlas.

In this post we are going to see how to send a scheduled message by the bot, using the Triggers in the Atlas Platform. If you read the first part of this post you already have an Atlas account.

Access to the Atlas platform, open your project and go to the App Services tab. In the left menu you will find the Triggers menu voice, under Build.

Clicking on Add a Trigger you can set the parameters for the new Trigger. Set the following parameters:

  • Trigger Type: Select Scheduled.
  • Name: Insert the name you prefer for this trigger.
  • Enabled: :)
  • Skip Events on Re-enable: Leave it disabled.
  • Schedule Type: Here you can set the scheduling, click on Advanced you ca insert a CRON expression to set the schedule. For example, if you want your bot saying goodmorning to all the channel members at 8 a.m. you can set the following expression: 0 8 * * * . In the box under the expression you can see all the next events and check if the expression matches your desired scheduling.
  • Select an Event Type: Select Function
  • Function: Select New Function

Selecting New Function a pane will expand under the Function one. Set the Function Name (e.g. goodMorningFunc) and put the following code in the Function Box:

exports = function() {
  const chatId = // Put here your chat id, remember the ; :)
  const botId = //put here your bot token

  let Client = require('node-rest-client').Client;
  let client = new Client();
  
  const intro = 'Good Morning - ';
  const phrases = [
    'It\'s Sunday, have a nice and relaxing day',
    'Monday, grab a coffee and take it easy',
    'Scientific studies affirm that Tuesday is a very productive day :)',
    'Wednesday! Here I come!',
    'Thursday, Just a little farther!',
    "It\'s Friday I\'m in love <3",
    'Saturday :D have fun and chill!'
    ];
    
  let today = new Date().getDay();
  
  let args = { 
      parameters: {
        chat_id: chatId, 
        text: intro + phrases[today]
      }
  };
  let url = 'https://api.telegram.org/bot' + botId + '/sendMessage';
  client.post(url, args, function (data, response) {
      console.log("answer() - ok: ", data.ok);
      console.log("answer() - error code: ", data.error_code);
      console.log("answer() - description: ", data.description);
      //console.log("answer() - response: ", response);
  });
};

As usual, Save and REVIEW DRAFT & DEPLOY to publish.

The previous script sends every day a different message, based on the day, starting with “Good Morning - " plus the daily phrase, e.g. “Good Morning - It's Sunday, have a nice and relaxing day” for the Sunday, etc.

The code is pretty straightforward, I don’t think it’s worth explaining it.

If you want to test if everything works, you can change the scheduling time, from the Triggers view, and check if the messages are correctly sent to your channel.

So, my tutorial about how to implement a simple Telegram bot ends here, you can explore Atlas features to implement new behaviors for your bot. You could use the MongoDB database to store data or implement other features for your bot. Always check what’s included in the free tier, otherwise, just upgrade your plan, and enjoy this wonderful platform.

I hope you enjoyed this post, if you want to share it to your contacts on the social media, you can do it by clicking on the social icons at the beginning of this post, otherwise you can copy the url and share it wherever you like.

If you want to reach me, you will find all my contacts in the About page. Sooner or later I will enable comments on my posts, in the meantime, if you have questions, contact me, I’ll be happy to answer.