How to set up a Telegram Bot using Node.js and node-telegram-bot-api
-
I will use www.openshift.com because it offers a free plan.
-
Sign in and click on “Add Application” or “Create your first application now”
-
Choose your public url and the region then click “Create Application” (this may take a few seconds)
-
In the next step click “Yes, help me get started” and insert your public SSH key. If you already have a key it can be found in “c:\Users\user.ssh\id_rsa.pub” if you are on windows, otherwise in “~/.ssh/id_rsa”
-
Press “Save”
-
Enter the nodejs folder
-
Open the file
server.js
and substitute the code with the example provided by https://github.com/yagop/node-telegram-bot-api
var TelegramBot = require('node-telegram-bot-api');
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
var bot = new TelegramBot(token, {polling: true});
// Matches /echo [whatever]
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
});
// Any kind of message
bot.on('message', function (msg) {
var chatId = msg.chat.id;
// photo can be: a file path, a stream or a Telegram file_id
var photo = 'cats.png';
bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
});
- Open Telegram and search for BotFather (https://telegram.me/botfather)
- Write
/newbot
- Write the name of your bot
- Write the username of your bot
- Copy your token and substitute it to “YOUR_TELEGRAM_BOT_TOKEN” in the file server.js
git add --all
git commit -am ”initial commit”
git push origin master
This may take few seconds
Note: ignore failure messages. These errors are reported because our app is not listening to any port, but we don’t need to.
Open your bot on Telegram and write /echo “write-something” and it should reply with the same message you wrote
Inspired by: https://github.com/yukuku/telebot