Answer the question
In order to leave comments, you need to log in
Telegram bot on Heroku runs for 60 seconds. Why?
This is my first time deploying a bot to Heroku.
There is the simplest bot that can respond to /start /info commands.
In the root folder, the file with the bot itself is called - bot.py
- an empty file __init__.py
- Procfile with the text: web: python3 bot.py
- requirements.txt with the text (created using pip freeze >> requirements.txt ):
certifi==2018.4.16
chardet==3.0.4
Click==7.0
Flask==1.0.2
idna==2.6
itsdangerous==1.1.0
Jinja2==2.10
MarkupSafe==1.1.0
pyTelegramBotAPI==3.6.4
six==1.11.0
telebot==0.0.3
urllib3==1.22
virtualenv==16.4.0
Werkzeug==0.14.1
xlrd==1.1.0
xlutils==2.0.0
xlwt==1.3.0
#!/usr/bin/python
# -*- coding: utf-8 -*-
import telebot
import os
from flask import Flask, request
import logging
bot = telebot.TeleBot("593642481:AAEuoLHI.....")
@bot.message_handler(commands=['start'])
def handle_text(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True,False)
user_markup.row('/start','/info')
start_text = str('Привет, '+message.from_user.first_name+'!\nЯ бот на Heroku.')
bot.send_message(chat_id=1154965888, text=start_text, parse_mode='Markdown')
if "HEROKU" in list(os.environ.keys()):
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
server = Flask(__name__)
@server.route("/bot", methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url="https://test-new-new.herokuapp.com")
return "?", 200
server.run(host="0.0.0.0", port=os.environ.get('PORT', 60))
else:
bot.remove_webhook()
bot.polling(none_stop=True)
2019-02-19T21:31:24.974014+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=good-bot-vera-ira.herokuapp.com request_id=45113a39-a143-422f-b392-811d30e1c4f7 fwd="188.242.4.23" dyno= connect= service= status=503 bytes= protocol=https
2019-02-19T21:31:25.346789+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=good-bot-vera-ira.herokuapp.com request_id=1c12d8be-4492-432d-9b10-57bb0deb1fb3 fwd="188.242.4.23" dyno= connect= service= status=503 bytes= protocol=https
2019-02-19T21:35:27.579713+00:00 heroku[web.1]: State changed from crashed to starting
2019-02-19T21:35:30.621337+00:00 heroku[web.1]: Starting process with command `python3 bot.py`
2019-02-19T21:36:31.313147+00:00 heroku[web.1]: State changed from starting to crashed
2019-02-19T21:36:31.089802+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-02-19T21:36:31.089987+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-02-19T21:36:31.157413+00:00 heroku[web.1]: Process exited with status 137
Answer the question
In order to leave comments, you need to log in
Use webhook: How to setup webhook for Telegram bot (python3 + pyTelegramBotAPI) on Heroku hosting?
And use port 5000 and not 60:
This is how it works for me:
import telebot
import os
from flask import Flask, request
bot = telebot.TeleBot("593642481:AAEuoLHI.....")
server = Flask(__name__)
@bot.message_handler(commands=['start'])
def handle_text(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True,False)
user_markup.row('/start','/info')
start_text = str('Привет, '+message.from_user.first_name+'!\nЯ бот на Heroku.')
bot.send_message(chat_id=1154965888, text=start_text, parse_mode='Markdown')
@server.route('/' + tokenBot.TOKEN, methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url='https://test-new-new.herokuapp.com/' + tokenBot.TOKEN)
return "!", 200
if __name__ == '__main__':
server.debug = True
server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))
dig here:
Heroku waits for the process to open the port and listen to it, the process apparently does not, and Heroku considers the launch to be unsuccessful.
You need to either find in Heroku where this behavior is configured and disable the check, or set $PORT to the desired port if the process is listening on some port.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question