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

Sample Bot ready to be hosted on Heroku #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/web-hosted/heroku/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn -b 0.0.0.0:$PORT app:app
30 changes: 30 additions & 0 deletions examples/web-hosted/heroku/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
from flask import Flask, request
import telepot

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

app = Flask(__name__)
TOKEN = os.environ['PP_BOT_TOKEN'] # put your token in heroku app as environment variable
SECRET = '/bot' + TOKEN
URL = '' # paste the url of your application

UPDATE_QUEUE = Queue()
BOT = telepot.Bot(TOKEN)

def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
BOT.sendMessage(chat_id, 'hello!')

BOT.message_loop({'chat': on_chat_message}, source=UPDATE_QUEUE) # take updates from queue

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

BOT.setWebhook() # unset if was set previously
Copy link
Contributor

@GaryBeez GaryBeez Sep 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of Sept 2016, Telegram seems to limit the rate of webhook setting to about 1/sec, raising a TooManyRequestsError (code 429) if it goes faster than that. Inserting a 1 or 2 second sleep between calls of setWebhook() fixes this.
Or just remove the first setWebhook() altogether, since there's no need to unset it before setting a new one.

BOT.setWebhook(URL + SECRET)
3 changes: 3 additions & 0 deletions examples/web-hosted/heroku/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flask
telepot
gunicorn
1 change: 1 addition & 0 deletions examples/web-hosted/heroku/runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.5.1