Building a Telegram bot Part II: Coding the bot (Node.js)

in #steemstem6 years ago (edited)


In this second tutorial, I am sharing about the coding part to make the telegram bot. In the previous video, I share about getting the necessary keys to use to make this bot; now in this tutorial, I will make use of the keys to create the bot.

About this tutorial

This is a tutorial of making a telegram bot on top of steem blockchain. The bot will watch for all the post on steemit and when there is a post with a tag of "contest" and "teammalaysia", it till notify out in telegram.

This bot solves the issue of not noticing a certain contest being announced, and when you want to participate it but it is over.

The dependencies:

Setup the bot

  • install all dependencies (npm install or yarn install)

  • create a .env file (refer to .env.sample )

CHANNEL_ID=-100<CHANNEL_ID>
BOT_KEY=<API_KEY>
  • depends on what tag you want to watch. (in this example, I use "teammalaysia" and "contest")
if (
      tags.includes('contest') &&
      tags.includes('teammalaysia')
    )
  • To run it, just node index.js.

For production, you can use the PM2 library.

Full source code

const telegram = require('node-telegram-bot-api');
const steem = require('steem');
const dotenv = require('dotenv');

dotenv.config();

const bot = new telegram(process.env.BOT_KEY, {
  polling: true
});

steem.api.streamTransactions('head', function(err, result) {
  if (err) {
    return;
  }

  let txType = result.operations[0][0];
  let txData = result.operations[0][1];

  // Check a post and it is not being resteem
  if (txType === 'comment' && txData.parent_author === '') {
    let tags;

    try {
      tags = JSON.parse(txData.json_metadata).tags;
    } catch (e) {
      return;
    }

    if (
      tags.includes('contest') &&
      tags.includes('teammalaysia')
    ) {
      let link = `@${txData.author}/${txData.permlink}`;

      bot.sendMessage(
        process.env.CHANNEL_ID,
        `https://steemit.com/${link}`
      );
    }
  }
});

▶️ DTube
▶️ IPFS
Sort:  

恭喜你!您的这篇文章入选 @justyy 今日 (2018-03-26) 榜单 【优秀被错过的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。

Congratulations! This post has been selected by @justyy as today's (2018-03-26) 【Good Posts You May Miss】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^

Thank you for sharing your posts with us. This post was curated by TeamMalaysia as part of our community support. Looking forward for more posts from you.

To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach

Vote TeamMalaysia witness bitrocker2020 using this link vote bitrocker2020 witness

Resteem a 1250 + Seguidor. Enviar 0.250 SBD o 0,300 STEEM a @ music-curation (URL como memo) + 16 votos a favor

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 57614.45
ETH 3097.31
USDT 1.00
SBD 2.57