如何修复:“缺少1个必需的位置参数:'args'”,使用python-telegram-bot

时间:2019-06-20 19:01:06

标签: python telegram-bot python-telegram-bot

我正在使用python-telegram-bot库创建电报bot,我想包含一个处理参数的处理程序。

this之后,我使用pass_args=True指令添加了新的处理程序,但没有成功

这是我到目前为止写的:

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import logging
[...]
def parameters(bot, update, args):
    user_says = " ".join(args)
    update.message.reply_text("You said: " + user_says)

[...]

def main():
    updater = Updater(TOKEN, use_context=True)

    d = updater.dispatcher

    # Handlers
    d.add_handler(CommandHandler("start", start))
    d.add_handler(CommandHandler("parameters", parameters, pass_args=True))
    d.add_handler(CommandHandler("help", helper))


    # Start the bot
    updater.start_polling()

    # Keep it active untile CTRL + C
    updater.idle()

问题在于,当我从电报启动/parameter <some_text>时,Bot给了我这个错误:

2019-06-20 20:57:19,823 - telegram.ext.dispatcher - ERROR - An uncaught error was raised while processing the update
Traceback (most recent call last):
  File "/home/zzz/aaaa/bot/venv/lib/python3.7/site-packages/telegram/ext/dispatcher.py", line 333, in process_update
    handler.handle_update(update, self, check, context)
  File "/home/zzz/aaaa/bot/venv/lib/python3.7/site-packages/telegram/ext/handler.py", line 117, in handle_update
    return self.callback(update, context)
TypeError: parameters() missing 1 required positional argument: 'args'

我做错了什么?
我正在使用最新的Beta(python-telegram-bot==12.0.0b1)版本。

2 个答案:

答案 0 :(得分:0)

参数中可能缺少参数

d.add_handler(CommandHandler("parameters", parameters(bot, update, args), pass_args=True))

答案 1 :(得分:0)

解决方案始终在python电报组内部

telegram.org /#/ im?p = @ pythontelegrambotgroup给我解决方案

 d.add_handler(CommandHandler("parameters", parameters, pass_args=True))

和功能

def parameters(update, context):
    user_says = " ".join(context.args)
    update.message.reply_text("You said: " + user_says)

args在context.args内部