I want to host a telegram bot made with python wrapper for telegram API telepot Following this tutorial with Flask it works, but can only serve one user. Multiple users will end up accessing the same instance of the bot. Hence I want to use the DelegatorBot example given by the telepot documentation.
However, I end up with this error in my webhook function: TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'ErrorLogFile'
from flask import Flask, request
import telepot
from telepot.delegate import per_chat_id, create_open, pave_event_space
import urllib3
import sys
proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))
try:
from Queue import Queue
except ImportError:
from queue import Queue
class MessageCounter(telepot.helper.ChatHandler):
def __init__(self, *args, **kwargs):
super(MessageCounter, self).__init__(*args, **kwargs)
self._count = 0
def on_chat_message(self, msg):
self._count += 1
self.sender.sendMessage(self._count)
TOKEN = '<My TOKEN>'
secret = '<Random generated hash>'
app = Flask(__name__)
update_queue = Queue() #channel between `app` and `bot`
bot = telepot.DelegatorBot(TOKEN, [
pave_event_space()(
per_chat_id(), create_open, MessageCounter, timeout=10),
])
@app.route('/{}'.format(secret), methods=["GET", "POST"])
def pass_update():
update = request.get_json()
if "message" in update:
text = update["message"]["text"]
chat_id = update["message"]["chat"]["id"]
bot.sendMessage(chat_id, "From the web: you said '{}'".format(text))
update_queue.put(request.data) #pass update to bot
return 'OK'
bot.setWebhook("<my pythonanywhere website url here>/{}".format(secret), max_connections=1)
bot.message_loop(source=update_queue) #take updates from queue