您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

51 行
1.4KB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from flask import Flask, request, json
  4. import markovify
  5. app = Flask(__name__)
  6. # Get raw text as string.
  7. with open("peejay.txt") as f:
  8. text = f.read()
  9. # Build the model.
  10. text_model = markovify.Text(text)
  11. @app.route('/', methods=['POST', 'GET'])
  12. def index():
  13. """Handles an event from Hangouts Chat."""
  14. if request.method == 'GET':
  15. return json.jsonify({'message': 'Hello there!'})
  16. event = request.get_json()
  17. if event['type'] == 'ADDED_TO_SPACE' and event['space']['type'] == 'ROOM':
  18. text = 'Thanks for adding me to "%s"!' % event['space']['displayName']
  19. return json.jsonify({'text': text})
  20. elif event['type'] == 'MESSAGE':
  21. text = text_model.make_short_sentence(280)
  22. return json.jsonify({
  23. 'cards': [{
  24. 'sections': [{
  25. 'widgets': [{
  26. 'textParagraph': {
  27. 'text': text
  28. }
  29. }]
  30. }]
  31. }]
  32. })
  33. else:
  34. return
  35. @app.errorhandler(500)
  36. def server_error(e):
  37. # Log the error and stacktrace.
  38. logging.exception('An error occurred during a request.')
  39. return 'An internal error occurred.', 500