jeudi 23 juin 2016

hosting python telegram bot on heroku


I have a .py file with some code to run a telegram bot and it works fine on my local machine but when i push my code into heroku it doesn't work, i mean the file is not running to receive and send messages but when i enter the bash in heroku and run the .py file manually it works fine.
I apply some changes in Procfile but i do not know how to tell heroku to run the .py file automatically.
I also tried to wrap my bot code inside a flask app and again it works perfect on local machine, but on the heroku the flask requests handled successfully but the bot inside the code doesn't work again.
Here is the code:

from flask import Flask
import time
import os
import telepot
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, PickleType, String
from sqlalchemy.orm import sessionmaker
from flask.ext.heroku import Heroku

app = Flask(__name__)
app.config['SECRET_KEY'] = "random string"
heroku = Heroku(app)

@app.route('/')  # the requests handled successfully!
def hello_world():
    return 'Hello World!'

# the bot code deleted to simplify

uri = os.environ.get('DATABASE_URL')
engine = create_engine(uri)
Base = declarative_base()


if __name__ == '__main__':
    Base.metadata.create_all(engine)
    global session
    Session = sessionmaker(bind=engine)
    session = Session()

    TOKEN = 'token'

    bot = MyBot(TOKEN)  # the bot stuff here
    bot.message_loop()  # but the bot stuff just doesn't work

    # i also removed incoming 2 lines and let it be the default but has no effect 
    port = int(os.environ.get("PORT", 5000))  

    app.run(host='0.0.0.0', port=port)
    while 1:
        time.sleep(10)

    engine.dispose()

I checked the log from heroku there was no error, but the bot code doesn't work.
So what is the problem in the "flask wrapped" bot AND how can i just simply tell the heroku to run a python file and let it be?


Aucun commentaire:

Enregistrer un commentaire