Telegram ChatBot in Node Js
A simple JS Intrepreter.
The idea was to create a javascript intrepreter
(not sure if it is the right word) for mobile devices.It occured to me that ,if I have to try some javascript snippets real quick,I had no option other than running a node instance in my shell or console
of my chrome browser.All this options are viable if i have access to a system.But what if you dont have access to as system and all you have is your mobile phone.So I have combined to basic ideas to build a javascript intrepreter
(Again not sure if it is the right word) for mobile devices.
Concepts
By combining these two simple concepts , we will build a simple chatbot that accepts javascript code snippets and returns the output.
Step 1 - Telegram ChatBot Server
To build a chatbot in Telegram
you have to take the help of BotFather
(Father of all Bots in Telegram
).
Find BotFather bot in telegram and use the command /newbot
to create a bot.
Once the bot is created successfully,you will be returned with a token your token
that is required to authorize the bot and send requests to the Bot API.Once the bot is created we have to build a server that would listen to the bot and sends the reply.
Step 2 - Building a Server
Install the following packages
npm init
This would create your package.json
file.Install these packages below
npm install express dotenv telegraf
- Telegraf- Wrapper for Telegram API’s.
- dotenv - Helps to read environemnt variables
- express - Serves a static page (Not Necessary)
Install babel, since we will be writing code in ES6
npm install babel-cli babel-core babel-preset-env --save-dev
Create an .env
file where we wil be storing our telegram token
// .env
ENV_BOT_TOKEN='your token'
Create a src
folder in the root directory and create ‘server.js`.This would be our server that would he handling the request from our telegram bot.
The Folder Structure would look like this now
-node_modules
-src
-server.js
-.env
-package.json
-index.html
Let the coding begin!!!!!!!!!!
require('dotenv').config()
const Telegraf = require('telegraf');
const app = new Telegraf(process.env.ENV_BOT_TOKEN);
The dotenv
module will read the environment variables from our .env
file and add it to out process.env
.So each entry in our .env
file can be accessed by process.env.<name>
.Now we create a new instance of Telegraf
with our token that we got during bot creation.
app.hears('hi',(ctx)=>{
return ctx.reply('Hello')
})
Try running our server.js
and chat with out bot from Telegram
.Type in “hi” and you would get response “Hello”
Good now you have created your chatbot,lets bring in the next concept in - Node VM. Node provides an API,a sandbox which would enable us to execute javascript and get the output.Please read the documentation for better understanding. So what we are going to do is ,accept the text from our chatbot,pass it to our VM and return the result back to our chatbot
import vm from 'vm';
import util from 'util
vm.runInContext("var x =1", sandbox);//sandbox defines the context in which it has to run.
Clubed all those together , this would bring our JS Intrepreter chatbot to life.
Please see the full source code in Github
This would have still many flaws and is under development You can search for chatbot in telegram by the name `jscontextbot’.The server is deployed in heroku,dyno would be sleeping.In case you did not get any response from my bot,never hesitate to wake him up here