Python-telegram-bot: Suggest adding a pass-custom-variable argument to callback function of a Handler

Created on 11 Feb 2018  Â·  3Comments  Â·  Source: python-telegram-bot/python-telegram-bot


Since this is not a bug reporting, I'll skip the standard format.

I'm working on a project that'll use both python-telegram-bot and paho-mqtt. The paho-mqtt library will be instantiated as mqttHandler and I can use this variable to subscribe, publish message from or to a specific channel in the telegram-bot callback function. The code is like:

def help(bot, update):
    update.message.reply_text('Help!')

mqttHandler = mqtt(host, port)
updater = Updater(token=telegramBotSecretKey)
dp = updater.dispatcher
dp.add_handler(CommandHandler("help", help))

I need to use mqttHandler in help function but a global variable here is not a good idea. So maybe it's better to provide an argument in the CommandHandler to pass some custom variables to the callback function, like this:

dp.add_handler(CommandHandler("help", help, custom_variable=mqttHandler))

and this in callback function:

def help(bot, update, custom_variable):
    custom_variable.publish(topic="test", message="Receive in callback")
    update.message.reply_text('Help!')

I noticed there's a pass_user_data argument in Handler class, but that's not what I mean in this issue.

Thank you guys.

Most helpful comment

@dangyuluo
In contrary to languages like C, python offers simple mechanisms for adding (extra) context to callback functions, therefor making the need for an explicit context redundant.

The most simple approach I can think of is by using functools.partial(), but there are other approaches like using lambda as @nmlorg suggested or creating (and instantiating) a class which holds the context.

A reduced example for using functools.partial():

from functools import partial

def help_cb(bot, update, ctx):
    pass

dp.add_handler(CommandHandler("help", partial(help_cb, ctx=custom_variable)))

As of the above I don't see any need to add such functionality to ptb, mostly as it will just add unnecessary levels of code.
That said, we always welcome community contribution. If you find the documentation/examples lacking and would like to see them improved, it would be nice if you can send a PR to update echobot2.py (for example) or just update the wiki.

All 3 comments

Couple ways to do this:

class MyBot(object):
    def __init__(self, mqttHandler):
        self.mqttHandler = mqttHandler

    def help(self, unused_bot, update):
        self.mqttHandler.publish(topic='test', message='Receive in callback')
        update.message.reply_text('Help!')


def main():
    mqttHandler = mqtt(host, port)
    my_bot = MyBot(mqttHandler)
    updater = Updater(token=telegramBotSecretKey)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler('help', my_bot.help))

```python
def help(unused_bot, update, mqttHandler):
mqttHandler.publish(topic='test', message='Receive in callback')
update.message.reply_text('Help!')

def main()
mqttHandler = mqtt(host, port)
updater = Updater(token=telegramBotSecretKey)
dp = updater.dispatcher
dp.add_handler(CommandHandler('help', lambda bot, update: help(bot, update, mqttHandler)))

@nmlorg You are right. These are all workarounds. Now I'm just assign mqttHandler to updater.bot as a property and call it in callback function.

@dangyuluo
In contrary to languages like C, python offers simple mechanisms for adding (extra) context to callback functions, therefor making the need for an explicit context redundant.

The most simple approach I can think of is by using functools.partial(), but there are other approaches like using lambda as @nmlorg suggested or creating (and instantiating) a class which holds the context.

A reduced example for using functools.partial():

from functools import partial

def help_cb(bot, update, ctx):
    pass

dp.add_handler(CommandHandler("help", partial(help_cb, ctx=custom_variable)))

As of the above I don't see any need to add such functionality to ptb, mostly as it will just add unnecessary levels of code.
That said, we always welcome community contribution. If you find the documentation/examples lacking and would like to see them improved, it would be nice if you can send a PR to update echobot2.py (for example) or just update the wiki.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YHporkar picture YHporkar  Â·  3Comments

jsmnbom picture jsmnbom  Â·  4Comments

mhipo1364 picture mhipo1364  Â·  5Comments

s-chizhik picture s-chizhik  Â·  4Comments

mzsalam picture mzsalam  Â·  5Comments