DEV Community

Cover image for Deploy telegram bot to heroku for free
Daniil Pankov
Daniil Pankov

Posted on • Updated on

Deploy telegram bot to heroku for free

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:
chrome_IyMPGdilzQ

Give it a name, choose location you prefer, then scroll down and choose GitHub and Repo with your bot: chrome_ZMN6JLPu0u

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:

4

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...'))
Enter fullscreen mode Exit fullscreen mode

⚠ 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

Open your app setting:
5

Scroll to config vars:
6

Add your variable, it should have same name, as inside your app:
7


Thanks for reading 💛

Oldest comments (0)