Try to create a Telegram bot to auto get current Japan and Unite State currency and reply.
Base on Node.js
- request
- cheerio
- node-telegram-bot-api
- heroku-cli && settings
- later.js
-
Send request && get currency what we want
Request plugin can let you send request to web pages, and we use cheerio to find HTML element what we want like jQuery. After get data we want, we export result like a module.
-
Create Telegram account
-
Create your Telegram bot from @BotFather.
-
Get your Telegram bot token.
-
Test your bot API in browser. (API document)
-
Get current currency and send Message by Telegram bot.
-
Create Heroku account.
-
Create Heroku app.
-
Use Heroku enviroment config to replace your bot token, channel ID, chat ID...what you want to hide or should not open to public. (Yuo need not to install any things, after deploy it will work perfectly.)
process.env.token
-
Integrate get-currency and Telegram-bot
-
Set schedule to run your code.
const later = require('later'); const sched = later.parse.recur().onWeekday().on(9, 10, 11, 12, 13, 14, 15, 16, 17).hour(); later.setInterval(() => { // your code here. }, sched);
-
Create Procfile file
This is a file to let Heroku know what command it should run.
clock: node index.js
* clock is a type of process. Because our process is a running on schedule process, so we should use clock.
-
Deploy your code to Heroku.
-
How to start or stop Heroku app?
You can use Heroku-cli to command "heroku ps:scale clock=1 --app APP_Name" to start your app.
heroku ps:scale clock=1 --app APP_Name
So you can command "heroku ps:scale clock=0 --app APP_Name" to stop your app.
heroku ps:scale clock=0 --app APP_Name
If your process type is "worker", and you should command "heroku ps:scale worker=0 --app APP_Name". Similarly, if your process type is "web", and you should command "heroku ps:scale web=0 --app APP_Name".
-
Telegram bot request timeout.
Telegram bot must set webhook in new-bot, or your request will timeout.
var port = process.env.PORT || 8443; var host = process.env.HOST; var bot = new TelegramBot(token, { webHook: { port: port, host: host }});
-
Heroku Server will turn to sleep mode, when your code is do nothing.
Solution: turn process type from web or worker to clock.
When your app receives no traffic in a 30 minute period, the your app will sleep.
- Larry850806 inspire me to do this bot.
- My friends shiningjason1989's help!
- Add Unite State currency get and reply.
- Formate bot reply message beautyfully.
- Using JavaScript ES6 santax.