Forums

Issue with Telepot DelegatorBot

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

That looks like either your code or a library you're using is using Python 2 syntax and you're running it in Python 3. Find the line where the error is coming from and fix it, or switch to using Python 2.

I have ran the code both in python 2.7 and 3.5 and the same error occurs.

The error log points to the line: def pass_update():

That line does not have ">>" in it, so it can't be the source of the error. Have you reloaded your web app? Did you reload it after changing the Python version?

oh there was a line below that that goes: #print >> sys.stderr, "receiving input, updating queue" does the comment # not work?

I removed it, changed to 2.7, reloaded, but I still dont see any changes in my Telegram Bot. Changed back to 3.5 and reloaded, no difference either. Now error log has stopped logging so I cant tell what new errors might have popped up.

Comments work.

If there is nothing is the error log, then maybe you're not getting errors anymore.