In this telegram video tutorial, I will be sharing on the guide on creating a telegram bot on top of steem blockchain. This bot will broadcast out in the Telegram Channel, whenever someone posted on steem with the tag #teammalaysia and #contest.
This bot allow users/subscribers to know the contest at the moment of posting, and participate it immediately.
Intermediate
This video tutorial is separated into 2 parts, where the first part is to get the necessary keys to start the project and the second part is to code the bot.
There are 2 keys that we need to extract out before we start coding, which is the telegram bot API key and Channel ID.
To get the Bot API key, you just need to message BotFather on telegram, starting with the command /newbot. Follow the instruction given by BotFather and eventually he will give out a message and give you the key.
For this part, it requires you to use the telegram web interface. After you create a new Channel, add in the bot you created previously and made it an admin. To get the channel ID, just get the URL of the telegram channel, and extract out the first combination of numbers. Then, add -100 as a prefix in front of the numbers.
You can read through the guide on stack overflow
.env fileIn the .env file, it looks like this:
CHANNEL_ID=-100<CHANNEL_ID>
BOT_KEY=<API_KEY>
Start the project with npm init or yarn init.
In this tutorial, I will make use of 3 library:
.env file.To install, just run npm install --save node-telegram-bot-api steem dotenv or yarn add node-telegram-bot-api steem dotenv.
After installing the dependencies, we can just create variables out from the library.
const telegram = require('node-telegram-bot-api');
const steem = require('steem');
const dotenv = require('dotenv');
We import all the .env file with the function:
dotenv.config();
Then, create a new telegram variable by passing in the private API key in the .env file and connect to the telegram bot with the method of long polling.
const bot = new telegram(process.env.BOT_KEY, {
polling: true
});
Next, we need to watch the Steem transaction. So, whenever there is a transaction (new post) on steem, it will watch for that. It is just like on Web interface, we use JS to watch when a user pressed a button.
steem.api.streamTransactions('head', function(err, result) {
if (err) {
return;
}
// Insert Code Here
})
From the result of the transaction, we look for 2 data that we need.
let txType = result.operations[0][0];
let txData = result.operations[0][1];
Then, we make sure that the post is a post (not a comment), and it is not a resteem
if (txType === 'comment' && txData.parent_author === '') {
// Insert Code Here
} else {
return;
}
Next, we try and see wether the post contain tags or not. If it doesn't, just return.
let tags;
try {
tags = JSON.parse(txData.json_metadata).tags;
} catch (e) {
return;
}
Next, we check wether the tags has #teammalaysia and #contest. If it doesn't, just return it.
if (
tags.includes('contest') &&
tags.includes('teammalaysia')
) {
// Insert Code Here
} else {
return:
}
Then, just extract out the author and permlink , and make it into steemit link.
let link = `@${txData.author}/${txData.permlink}`; // @superoo7/something
Then, we just need to telegram bot to send the message in the channel, by passing in the channel ID from .env and the steemit link.
bot.sendMessage(
process.env.CHANNEL_ID,
`https://steemit.com/${link}`
);
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}`
);
}
}
});