Skip to content
This repository has been archived by the owner on Jun 2, 2019. It is now read-only.

How do I deploy a bot on Heroku? #109

Closed
darshitpp opened this issue Jun 24, 2016 · 6 comments
Closed

How do I deploy a bot on Heroku? #109

darshitpp opened this issue Jun 24, 2016 · 6 comments

Comments

@darshitpp
Copy link

This might be a noob question, and not a technical issue, but I'm having a real difficult time wrapping my head around deployment on Heroku.

I tried using Webhooks, and if I'm right, they are used for just pushing app updates(?). It would be great if one could point me towards some easy to learn tutorials about deploying the bot on Heroku.

Sorry if this question doesn't suit this forum.

Also, kudos to @nickoala for this Python wrapper!

@zumoshi
Copy link
Contributor

zumoshi commented Jun 24, 2016

hello,
you can take a look at #96 . it has a sample code for heroku.

about webhooks, each time a message is sent to the bot, telegram sends a request to your webhook with the message object. so, yes they are basically used for pushing updates to your bot.

i havent personally used heroku but it shouldnt be different from generic webhook bots so you can also take a look at our webhook examples here

@darshitpp
Copy link
Author

Yes, I have tried to emulate #96 , but it seems the bot does not handle any requests whatsoever. It displays the I am Listening message, but even after passing the commands to the bot, I don't get the Got command as shown in some skeleton examples.

import sys
import time
import random
import datetime
import telepot
import os
from flask import Flask, request
from telepot.delegate import per_from_id, create_open

try:
    from Queue import Queue
except ImportError:
    from queue import Queue

"""
My bot logic here
"""

PORT = int(os.environ.get('PORT', 5000))
TOKEN = os.environ['PP_BOT_TOKEN']  # put your token in heroku app as environment variable
SECRET = '/bot' + TOKEN
URL = " "

app = Flask(__name__)
update_queue = Queue()

bot = telepot.DelegatorBot( TOKEN, [(per_from_id(), create_open(BotFunction, timeout=10000000000000)),])

print 'I am listening ...'

bot.message_loop(run_forever=True, source=update_queue)

@app.route( SECRET, methods=['GET', 'POST'])
def pass_update():
    update_queue.put(request.data)  # pass update to bot
    return 'OK'

if __name__ == '__main__':
    bot.setWebhook()
    bot.setWebhook(URL + SECRET)
    app.run(host='0.0.0.0', port=PORT, debug=True)

Can you point me where I'm going wrong here? I run the local development version of the bot without the Webhooks and Flask, and it works flawlessly.

@zumoshi
Copy link
Contributor

zumoshi commented Jun 24, 2016

i think the problem is that you never initiate your flask app ... or set the webhook for that matter.
the following line blocks the execution, so anything below it does not run:

bot.message_loop(run_forever=True, source=update_queue)

could you move this line to the end of file and see if the problem is solved ?


also i would reduce the timeout that you set, your current timeout really acts as a memory leak, it initiates a new class instance for each user but never removes it.
i also don't see the class BotFunction defined or imported in your snippet, i assume that the main code is correct and only partially copied here? otherwise that could also lead to an error.

@darshitpp
Copy link
Author

darshitpp commented Jun 24, 2016

Thanks, yes, it seems the position of bot.message_loop(run_forever=True, source=update_queue) was a part of the problem. I still tried to deploy, and through the debug messages I put in the code, I noticed that the code inside if __name__ == '__main__': wasn't getting executed. So, I set the bot.setWebhook(URL + SECRET) outside the main, but the app.run(host='0.0.0.0', port=PORT, debug=True) doesn't work even if get it outside the main. Please check the code below for clarity.

bot.setWebhook(URL + SECRET)
#app.run(host='0.0.0.0', port=PORT, debug=True)  <-This doesnt work. The heroku logs show the error H10: App crashes.
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=PORT, debug=True) # The point of execution simply doesn't enter this part.

The BotFunction is really the main code of the bot and it certainly does not contain anything which relates to the deployment logic. As I said, the bot works perfect locally, and it's only the deployment giving me the problems.

@zumoshi
Copy link
Contributor

zumoshi commented Jun 24, 2016

that is more of a flask problem than telepot.
as long as the line with update_queue.put(request.data) is called on request telepot should work.
i have not used heroku myself so i cant help you much, but try googling how to run a flask app on heroku.
or see if the following link helps.

https://community.nitrous.io/tutorials/deploying-a-flask-application-to-heroku

if all that failed try opening an issue with flask or using another http library.
when you got your flask app to work, and the updates were added to the queue, if you still had problems, we can try to diagnose it further.

@nickoala nickoala closed this as completed Jul 4, 2016
@nickoala
Copy link
Owner

nickoala commented Jul 4, 2016

One thing I want to add. You should not need run_forever=True in this case, because app.run() already keeps the program running. bot.message_loop(source=update_queue) should be enough.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants