Deploy telegram bot to heroku for free
Daniil Pankov
Posted on August 21, 2021
I used telegraf, but you can use any other framework and language
Hello there, you created your telegram bot and want to deploy it somewhere? Let me help you, I already done this before:
ā¶ Bot itself - https://t.me/WishBasket_bot
ā¶ Source code - https://github.com/T-Damer/wishBasket-bot
First steps
Create repository on github and upload your bot to main
branch, like I did.
Allright, now let's go to Heroku Dashboard. Create app, if you still haven't done so:
Give it a name, choose location you prefer, then scroll down and choose GitHub and Repo with your bot:
I also recommend checking auto-deploy, so when you upload new changes to your repo, it will deploy them automatically
š Nice, you completed step one š
Easy, but...
Okay, your bot will work for a while, or maybe not... You may see errors over there:
Let's go straight through the problems you will face
Can't get '/' : code=H10 desc="App crashed"
and Can not get '/'
and Can not get 'favicon.ico'
This error happens, because Heroku don't know, that you're deploying a bot, Heroku thinks, that you want to make a web-page.
To awoid Can not get '/'
, simply add express (or any other server-app) to your App.js
or App.ts
like this:
(Yes, I use TypeScript, no worries, you can do this with JS š)
import express = require('express')
// ===Your bot logic here
// Start app for Heroku
const app = express()
app.use(express.static('public'))
app.get('/', function (req, res) {
res.send(
"<h1>Hello There! You found <a href='https://t.me/wishbasket_bot'>@WishBasket_bot</a> backend</h1>"
)
})
// Start server
app.listen(process.env.PORT || 3000, () => console.log('Server is running...'))
ā Use process.env.PORT || 3000
, because Heroku uses it's own PORT, which will be taken from it's environment ā
Congratulations, you have successfully avoided first error š
Heroku started idling
Everything were good, but after 30 minutes heroku turned my bot off, it is not responding to any commands š¢
Chill, will fix it in a minute š¦
Consider looking here before avoiding this, you must know limitations
Quick answer: add your bot express url (mine was - https://wishbasket-bot.herokuapp.com/, click Open App
on Heroku Dashboard) to kaffeine.heroku, remember to setup badtime (time format is GTM!) It will ping your online page every 30 minutes, so your bot will never fall asleep ā
Long answer(s): well, better read answers here. You can use this NewRelic extension, but I personally recommend you using caffeine, because if you want to add extensions on Heroku, you must enter your credit card (which I could not do, although the data is correct, thanks Heroku š¤¦āāļø)
Congratulations on your bypassing restrictions š¾
Bonus. How to add secrets?
Well, I personally used MongoDB Cluster to manage database, I had special link with login and password to my database, hidden in .env.local
file, and used .dotenv
to use this file (never publish such a thing to the internet!)
So, I nedded to use this link in my bot, but it wasn't in my repo, what should I do?
Easy, watch me
Add your variable, it should have same name, as inside your app:
Thanks for reading š
Posted on August 21, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.