2017-08-02 101 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

謝謝!團隊訂購幫助! –