2017-09-19 6 views
0

さて、次のコードをコマンド/startコマンドのコマンドハンドラを追加します。

dp = updater.dispatcher 
dp.add_handler(CommandHandler('start', start)) 

私は1つのすべての上/download_video/download_musicなどのようなコマンドを処理しますハンドラを追加したいですハンドラ。

dp.add_handler(CommandHandler(r'^download', download)) 

しかしはずとしてそれは動作しません:私は考え

何かが、このでした!代わりに、非コマンド文字列を送信すると動作します/^download

どうすればよいですか?

答えて

0

source codeによれば、CommandHandlerパラメータとしてのみ文字列を渡すことができます。

すべてのコマンドを/downloadと呼ぶことをお勧めします。これはユーザーにとって使いやすいものです。あなたが行うことができますので

0

CommandHandlerは、入力として文字列のタプル/リストを受け付けます。

dp.add_handler(CommandHandler(('download_video', 'download_music'), download))

または

dp.add_handler(CommandHandler(['download_video', 'download_music'], download))

0

あなたは正規表現を使用してカスタムハンドラを作成することができます。たとえば、regexp_commandという名前の新しいファイルを作成します。次に、下に書かれたコードをコピーします。これで、ハンドラ引数としてregexpを使用できます。たとえば、dispatcher.add_handler(RegexpCommandHandler(r'start_[\d]+', start))

import re 
import warnings 

from telegram import Update 
from telegram.ext import Handler 


class RegexpCommandHandler(Handler): 
    def __init__(self, 
       command_regexp, 
       callback, 
       filters=None, 
       allow_edited=False, 
       pass_args=False, 
       pass_update_queue=False, 
       pass_job_queue=False, 
       pass_user_data=False, 
       pass_chat_data=False): 
     super(RegexpCommandHandler, self).__init__(
      callback, 
      pass_update_queue=pass_update_queue, 
      pass_job_queue=pass_job_queue, 
      pass_user_data=pass_user_data, 
      pass_chat_data=pass_chat_data 
     ) 

     self.command_regexp = command_regexp 

     self.filters = filters 
     self.allow_edited = allow_edited 
     self.pass_args = pass_args 

     # We put this up here instead of with the rest of checking code 
     # in check_update since we don't wanna spam a ton 
     if isinstance(self.filters, list): 
      warnings.warn('Using a list of filters in MessageHandler is getting ' 
          'deprecated, please use bitwise operators (& and |) ' 
          'instead. More info: https://git.io/vPTbc.') 

    def check_update(self, update): 
     if (isinstance(update, Update) 
      and (update.message or update.edited_message and self.allow_edited)): 
      message = update.message or update.edited_message 

      if message.text and message.text.startswith('/') and len(message.text) > 1: 
       command = message.text[1:].split(None, 1)[0].split('@') 
       command.append(
        message.bot.username) # in case the command was send without a username 
       print(command) 

       if self.filters is None: 
        res = True 
       elif isinstance(self.filters, list): 
        res = any(func(message) for func in self.filters) 
       else: 
        res = self.filters(message) 

       match = re.match(self.command_regexp, command[0]) 

       return res and (bool(match) and command[1].lower() == message.bot.username.lower()) 
      else: 
       return False 

     else: 
      return False 

    def handle_update(self, update, dispatcher): 
     optional_args = self.collect_optional_args(dispatcher, update) 

     message = update.message or update.edited_message 

     if self.pass_args: 
      optional_args['args'] = message.text.split()[1:] 

     return self.callback(dispatcher.bot, update, **optional_args)