мои функции, использующие args в python -telegram-bot, не работают должным образом (поэтому смущены) - PullRequest
0 голосов
/ 16 марта 2020

спасибо за ваше внимание, я совершенно сбит с толку:

вот две мои функции и команды, но когда я запускаю код, бот не отвечает мне, например, когда я говорю '/ cmd dir 'или' / cmd ipconfig 'или' / upload test.txt 'ничего не происходит.

def cmd_method(bot , update , args):

        chat_id = update.message.chat_id
        cmd = subprocess.check_output(args , shell = True)
        bot.sendMessage(chat_id , cmd)



def upload_method(bot , update , args):

        chat_id = update.message.chat_id
        document = open(args , "rb")
        bot.sendDocument(chat_id , document)
        document.close()


cmd = CommandHandler("cmd" , cmd_method , pass_args = True)
update.dispatcher.add_handler(cmd)

upload = CommandHandler("upload" , upload_method , pass_args=True )
update.dispatcher.add_handler(upload)

1 Ответ

0 голосов
/ 21 апреля 2020

Вы можете найти аргументы в CallbackContext.args на python -telegram-bot v12.

import subprocess
from telegram.ext import Updater, CommandHandler
from telegram import Bot

def cmd_method(update, context):
    if context.args:
        cmd = subprocess.check_output(context.args, shell=True)
        update.message.reply_text("{!s}".format(cmd.strip().decode()))
    else:
        update.message.reply_text("You need to specify a command to execute")

def upload_method(update, context):
    if context.args:
        chat_id = update.message.chat_id
        document = open(context.args[0] , "rb")
        bot.sendDocument(chat_id , document)
        document.close()
    else:
        update.message.reply_text("You need to specify a file to upload")

update = Updater(tgtoken, use_context=True)
bot = Bot(tgtoken)

cmd = CommandHandler("cmd", cmd_method)
update.dispatcher.add_handler(cmd)
upload = CommandHandler("upload", upload_method)
update.dispatcher.add_handler(upload)

update.start_polling()
update.idle()
...