選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

135 行
4.6KB

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. from flask import Flask, request, json
  4. import markovify
  5. app = Flask(__name__)
  6. def load_model(fname):
  7. # Get raw text as string.
  8. with open(fname) as f:
  9. text = f.read()
  10. # Build the model.
  11. return markovify.Text(text)
  12. models = {
  13. "peejay": load_model("peejay.txt"),
  14. "nithin": load_model("nithin.txt"),
  15. "vss": load_model("vss.txt"),
  16. "kunal": load_model("kunal.txt")
  17. }
  18. @app.route('/peejay', methods=['POST', 'GET'])
  19. def index():
  20. """Handles an event from Hangouts Chat."""
  21. if request.method == 'GET':
  22. return json.jsonify({'message': 'Hello there!'})
  23. event = request.get_json()
  24. if event['type'] == 'ADDED_TO_SPACE' and event['space']['type'] == 'ROOM':
  25. text = 'Thanks for adding me to "%s"!' % event['space']['displayName']
  26. return json.jsonify({'text': text})
  27. elif event['type'] == 'MESSAGE':
  28. text = models["peejay"].make_short_sentence(280)
  29. return json.jsonify({
  30. 'cards': [{
  31. 'sections': [{
  32. 'widgets': [{
  33. 'textParagraph': {
  34. 'text': text
  35. }
  36. }]
  37. }]
  38. }]
  39. })
  40. else:
  41. return
  42. @app.route('/vss', methods=['POST', 'GET'])
  43. def index():
  44. """Handles an event from Hangouts Chat."""
  45. if request.method == 'GET':
  46. return json.jsonify({'message': 'Hello there!'})
  47. event = request.get_json()
  48. if event['type'] == 'ADDED_TO_SPACE' and event['space']['type'] == 'ROOM':
  49. text = 'Thanks for adding me to "%s"!' % event['space']['displayName']
  50. return json.jsonify({'text': text})
  51. elif event['type'] == 'MESSAGE':
  52. text = models["vss"].make_short_sentence(280)
  53. return json.jsonify({
  54. 'cards': [{
  55. 'sections': [{
  56. 'widgets': [{
  57. 'textParagraph': {
  58. 'text': text
  59. }
  60. }]
  61. }]
  62. }]
  63. })
  64. else:
  65. return
  66. @app.route('/kunal', methods=['POST', 'GET'])
  67. def index():
  68. """Handles an event from Hangouts Chat."""
  69. if request.method == 'GET':
  70. return json.jsonify({'message': 'Hello there!'})
  71. event = request.get_json()
  72. if event['type'] == 'ADDED_TO_SPACE' and event['space']['type'] == 'ROOM':
  73. text = 'Thanks for adding me to "%s"!' % event['space']['displayName']
  74. return json.jsonify({'text': text})
  75. elif event['type'] == 'MESSAGE':
  76. text = models["kunal"].make_short_sentence(280)
  77. return json.jsonify({
  78. 'cards': [{
  79. 'sections': [{
  80. 'widgets': [{
  81. 'textParagraph': {
  82. 'text': text
  83. }
  84. }]
  85. }]
  86. }]
  87. })
  88. else:
  89. return
  90. @app.route('/nithin', methods=['POST', 'GET'])
  91. def index():
  92. """Handles an event from Hangouts Chat."""
  93. if request.method == 'GET':
  94. return json.jsonify({'message': 'Hello there!'})
  95. event = request.get_json()
  96. if event['type'] == 'ADDED_TO_SPACE' and event['space']['type'] == 'ROOM':
  97. text = 'Thanks for adding me to "%s"!' % event['space']['displayName']
  98. return json.jsonify({'text': text})
  99. elif event['type'] == 'MESSAGE':
  100. text = models["nithin"].make_short_sentence(280)
  101. return json.jsonify({
  102. 'cards': [{
  103. 'sections': [{
  104. 'widgets': [{
  105. 'textParagraph': {
  106. 'text': text
  107. }
  108. }]
  109. }]
  110. }]
  111. })
  112. else:
  113. return
  114. @app.errorhandler(500)
  115. def server_error(e):
  116. # Log the error and stacktrace.
  117. logging.exception('An error occurred during a request.')
  118. return 'An internal error occurred.', 500