server_readonly.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import chatterbot
  2. import cherrypy
  3. from chatterbot.conversation import Statement
  4. class Server:
  5. def __init__(self, template_key, template_chat, bots, badwords=()):
  6. self.t_key = template_key
  7. self.t_chat = template_chat
  8. self.bots = bots
  9. self.badwords = badwords
  10. @cherrypy.expose
  11. def index(self, key=""):
  12. if key not in self.bots.keys():
  13. return self.t_key
  14. return self.t_chat.replace("(*KEY_HERE*)", key)
  15. @cherrypy.expose
  16. def api(self, msg, key="EMPTY"):
  17. if key not in self.bots.keys():
  18. raise cherrypy.HTTPError(401, "کلید اشتباه!")
  19. msg_low = msg.lower()
  20. for badword in self.badwords:
  21. if badword in msg_low:
  22. raise cherrypy.HTTPError(403, "پیغام یک کلمه رکیک دارد: " + badword)
  23. res = self.bots[key].get_response(Statement(msg)).text
  24. print(msg, "/", res)
  25. return res
  26. def a_chatbot_pls(read_only=False):
  27. return chatterbot.ChatBot("Kalpak", read_only=read_only)
  28. if __name__ == "__main__":
  29. t_key = ""
  30. t_chat = ""
  31. with open("./index_key.html") as fp:
  32. t_key = fp.read()
  33. with open("./index_chat.html") as fp:
  34. t_chat = fp.read()
  35. bots = {
  36. "EMPTY": a_chatbot_pls(True)
  37. }
  38. keys = (
  39. "DrKhaliliba0",
  40. "BSJbi0",
  41. "DeChickenkiller00001",
  42. "AuntS1399",
  43. "DynamicMemoryAllocation"
  44. )
  45. for key in keys:
  46. bots[key] = a_chatbot_pls(True)
  47. badwords = ("کیر", "کون", "مادرجنه", "پدرسگ")
  48. print(bots.keys())
  49. print(badwords)
  50. conf = {"global":
  51. {
  52. "server.socket_host": "0.0.0.0",
  53. "server.socket_port": 57057
  54. }
  55. }
  56. cherrypy.quickstart(Server(t_key, t_chat, bots, badwords), "/", conf)