2017-08-02 55 views
0

我试图从python-telegram-bot==7.0.1使用CommandHandler,但是,它没有做我期望的任何事情。CommandHandler不能在Telegram bot库中工作

其实,我不能让任何状态:

# -*- coding: utf-8 -*- 

from __future__ import unicode_literals, division, print_function 
import logging 
import telegram 
from telegram.ext import CommandHandler, CallbackQueryHandler, MessageHandler, ConversationHandler, RegexHandler 
from telegram.ext import Updater, Filters 

# Set up Updater and Dispatcher 

updater = Updater(TOKEN) 
updater.stop() 
dispatcher = updater.dispatcher 

# Add logging 

logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.WARNING) 

TIME, NOTIME = range(2) 


def whiteboard(bot, update): 
    print(1) 
    bot.sendMessage(text="Send update", chat_id=update.message.chat.id) 
    bot.sendMessage(text=update.message.text, chat_id=update.message.chat.id) 
    print(type(TIME)) 
    return TIME 


def whiteboard_update(bot, update): 
    print(2) 
    bot.sendMessage(text=update.message.text, chat_id=update.message.chat.id) 
    return TIME 


def cancel(bot, update): 
    print(3) 
    bot.sendMessage(text=update.message.text, chat_id=update.message.chat.id) 
    bot.sendMessage(text="Это не время, а что то еще...", chat_id=update.message.chat.id) 
    return NOTIME 


def error(bot, update, error): 
    logging.critical('Update "%s" caused error "%s"' % (update, error)) 


def main(): 

    whiteboard_handler = CommandHandler("whiteboard", whiteboard) 
    dispatcher.add_handler(whiteboard_handler) 

    conv_handler = ConversationHandler(
     entry_points=[CommandHandler("whiteboard", whiteboard)], 
     states={ 
      TIME: [RegexHandler('^[0-9]+:[0-5][0-9]$', whiteboard_update), CommandHandler('cancel', cancel)], 
      NOTIME: [MessageHandler(Filters.text, cancel), CommandHandler('cancel', cancel)] 
     }, 
     fallbacks=[CommandHandler('cancel', cancel)], 
     ) 
    dispatcher.add_handler(conv_handler) 

    # log all errors 
    updater.dispatcher.add_error_handler(error) 

    # Poll user actions 

    updater.start_polling() 
    updater.idle() 


if __name__ == '__main__': 
    main() 

所以,/whiteboard返回它有什么,但任何文字和/或时间(例如1:11)没有得到我所需要的功能。

回答

0

只有可以执行的任何组的第一个处理程序才会被执行,而同一组的其他处理程序则不会。

在你的情况commandHandler和conversationHandler在同一个组中,当用户键入命令时,只执行commandHandler而不是conversationHandler(每个组只有一个处理程序按照你的顺序执行(如果它们被触发)写他们)。

,如果你想同时运行它们,你可以在两个不同的组这样割裂开来:

dispatcher.add_handler(whiteboard_handler, -1) 

加入“-1”作为放慢参数你说,它属于前一组的处理程序。

或者如果你不想在两个组中分割它们,你可以使用flow control,但它应该仅在目前为止的主分支中合并。要使用它,你必须提出“DispatcherHandlerContinue”异常

+0

谢谢!团队订购帮助! –