0

I'm trying to setup a bot which:

  1. Receives the keywords in /search_msgs userkey command from a TG group
  2. Search in DB for that userkey and send back appropriate text back

I'm getting two errors

  1. None type object has no attribute args, in callback_search_msgs(context), see code snippet
  2. AttributeError: 'int' object has no attribute 'job_queue', in search_msgs(update, context), see code snippet.

Telegram's official documents is way too difficult for me to read and understand. Couldn't find even one place where Updater, update, Commandhandler, context are all explained together with examples.

How to fix this code?

import telegram
from telegram.ext import Updater,CommandHandler, JobQueue

token = "Token"
bot = telegram.Bot(token=token)

# Search specific msgs on user request
def search_msgs(update, context):
    context.job_queue.run_once(callback_search_msgs, context=update.message.chat_id)


def callback_search_msgs(context):
    print('In TG, args', context.args)
    chat_id = context.job.context
    search_msgs(context, chat_id)



def main():
    updater = Updater(token, use_context=True)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler("search_msgs",search_msgs, pass_job_queue=True,
                                  pass_user_data=True))
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

1 Answer 1

2

Let me first try & clear something up:

Telegram's official documents is way too difficult for me to read and understand. Couldn't find even one place where Updater, update, Commandhandler, context are all explained together with examples.

I'm guessing that by "Telegram's official documents" you mean the docs at https://core.telegram.org/bots/api. However, Updater, CommandHandler and context are concepts of python-telegram-bot, which is one (of many) python libraries that provides a wrapper for the bot api. python-telegram-bot provides a tutorial, examples, a wiki where a lots of the features are explained and documentation.


Now to your code:

  1. In context.job_queue.run_once(callback_search_msgs, context=update.message.chat_id) you're not telling job_queue when to run the the job. You must pass an integer or a datetime.(date)time object as second argument.

  2. in

    def callback_search_msgs(context):
        print('In TG, args', context.args)
        chat_id = context.job.context
        search_msgs(context, chat_id)
    

    you are passing context and chat_id to search_msgs. However, that function treats context as an instance of telegram.ext.CallbackContext, while you pass an integer instead. Also, even if that worked, this would just schedule another job in an infinite loop.

Finally, I don't understand what scheduling jobs has to do with looking up a key in a database. All you have to do for that is something like

def search_msgs(update, context):
    userkey = context.args[0]
    result = look_up_key_in_db(userkey)
    # this assumes that result is a string:
    update.effective_message.reply_text(result)

To understand context.args better, have a look at this wiki page.


Disclaimer: I'm currently the maintainer of python-telegram-bot.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.