Guest User

Untitled

a guest
Jun 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import configparser
  2. import logging
  3.  
  4. import telegram
  5. from flask import Flask, request
  6. from telegram import ReplyKeyboardMarkup
  7. from telegram.ext import Dispatcher, CommandHandler, MessageHandler, Filters
  8.  
  9. from nlp.olami import Olami
  10.  
  11. # Load data from config.ini file
  12. config = configparser.ConfigParser()
  13. config.read('config.ini')
  14.  
  15. # Enable logging
  16. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  17. level=logging.INFO)
  18. logger = logging.getLogger(__name__)
  19.  
  20. # Initial Flask app
  21. app = Flask(__name__)
  22.  
  23. # Initial bot by Telegram access token
  24. bot = telegram.Bot(token=(config['TELEGRAM']['ACCESS_TOKEN']))
  25.  
  26. welcome_message = '親愛的主人,您可以問我\n' \
  27. '天氣,例如:「高雄天氣如何」\n' \
  28. '百科,例如:「川普是誰」\n' \
  29. '新聞,例如:「今日新聞」\n' \
  30. '音樂,例如:「我想聽周杰倫的等你下課」\n' \
  31. '日曆,例如:「現在時間」\n' \
  32. '詩詞,例如:「我想聽水調歌頭這首詩」\n' \
  33. '笑話,例如:「講個笑話」\n' \
  34. '故事,例如:「說個故事」\n' \
  35. '股票,例如:「台積電的股價」\n' \
  36. '食譜,例如:「蛋炒飯怎麼做」\n' \
  37. '聊天,例如:「你好嗎」'
  38. reply_keyboard_markup = ReplyKeyboardMarkup([['高雄天氣如何'],
  39. ['川普是誰'],
  40. ['今日新聞'],
  41. ['我想聽周杰倫的等你下課'],
  42. ['現在時間'],
  43. ['我想聽水調歌頭這首詩'],
  44. ['講個笑話'],
  45. ['說個故事'],
  46. ['台積電的股價'],
  47. ['蛋炒飯怎麼做'],
  48. ['你好嗎']])
  49.  
  50.  
  51. @app.route('/hook', methods=['POST'])
  52. def webhook_handler():
  53. """Set route /hook with POST method will trigger this method."""
  54. if request.method == "POST":
  55. update = telegram.Update.de_json(request.get_json(force=True), bot)
  56. dispatcher.process_update(update)
  57. return 'ok'
  58.  
  59.  
  60. def start_handler(bot, update):
  61. """Send a message when the command /start is issued."""
  62. update.message.reply_text(welcome_message, reply_markup=reply_keyboard_markup)
  63.  
  64.  
  65. def help_handler(bot, update):
  66. """Send a message when the command /help is issued."""
  67. update.message.reply_text(welcome_message, reply_markup=reply_keyboard_markup)
  68.  
  69.  
  70. def reply_handler(bot, update):
  71. """Reply message."""
  72. text = update.message.text
  73. reply = Olami().nli(text)
  74. update.message.reply_text(reply)
  75.  
  76.  
  77. def error_handler(bot, update, error):
  78. """Log Errors caused by Updates."""
  79. logger.error('Update "%s" caused error "%s"', update, error)
  80. update.message.reply_text('對不起主人,我需要多一點時間來處理 Q_Q')
  81.  
  82.  
  83. # New a dispatcher for bot
  84. dispatcher = Dispatcher(bot, None)
  85.  
  86. # Add handler for handling message, there are many kinds of message. For this handler, it particular handle text
  87. # message.
  88. dispatcher.add_handler(MessageHandler(Filters.text, reply_handler))
  89. dispatcher.add_handler(CommandHandler('start', start_handler))
  90. dispatcher.add_handler(CommandHandler('help', help_handler))
  91. dispatcher.add_error_handler(error_handler)
  92.  
  93. if __name__ == "__main__":
  94. # Running server
  95. app.run(debug=True)
Add Comment
Please, Sign In to add comment