Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

active_reply.py 3.0KB

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import time
  2. import web
  3. from channel.wechatmp.wechatmp_message import parse_xml
  4. from channel.wechatmp.passive_reply_message import TextMsg
  5. from bridge.context import *
  6. from bridge.reply import ReplyType
  7. from channel.wechatmp.common import *
  8. from channel.wechatmp.wechatmp_channel import WechatMPChannel
  9. from common.log import logger
  10. from config import conf
  11. # This class is instantiated once per query
  12. class Query:
  13. def GET(self):
  14. return verify_server(web.input())
  15. def POST(self):
  16. # Make sure to return the instance that first created, @singleton will do that.
  17. channel = WechatMPChannel()
  18. try:
  19. webData = web.data()
  20. # logger.debug("[wechatmp] Receive request:\n" + webData.decode("utf-8"))
  21. wechatmp_msg = parse_xml(webData)
  22. if (
  23. wechatmp_msg.msg_type == "text"
  24. or wechatmp_msg.msg_type == "voice"
  25. # or wechatmp_msg.msg_type == "image"
  26. ):
  27. from_user = wechatmp_msg.from_user_id
  28. message = wechatmp_msg.content
  29. message_id = wechatmp_msg.msg_id
  30. logger.info(
  31. "[wechatmp] {}:{} Receive post query {} {}: {}".format(
  32. web.ctx.env.get("REMOTE_ADDR"),
  33. web.ctx.env.get("REMOTE_PORT"),
  34. from_user,
  35. message_id,
  36. message,
  37. )
  38. )
  39. if (wechatmp_msg.msg_type == "voice" and conf().get("voice_reply_voice") == True):
  40. rtype = ReplyType.VOICE
  41. else:
  42. rtype = None
  43. context = channel._compose_context(
  44. ContextType.TEXT, message, isgroup=False, desire_rtype=rtype, msg=wechatmp_msg
  45. )
  46. if context:
  47. # set private openai_api_key
  48. # if from_user is not changed in itchat, this can be placed at chat_channel
  49. user_data = conf().get_user_data(from_user)
  50. context["openai_api_key"] = user_data.get(
  51. "openai_api_key"
  52. ) # None or user openai_api_key
  53. channel.produce(context)
  54. # The reply will be sent by channel.send() in another thread
  55. return "success"
  56. elif wechatmp_msg.msg_type == "event":
  57. logger.info(
  58. "[wechatmp] Event {} from {}".format(
  59. wechatmp_msg.Event, wechatmp_msg.from_user_id
  60. )
  61. )
  62. content = subscribe_msg()
  63. replyMsg = TextMsg(
  64. wechatmp_msg.from_user_id, wechatmp_msg.to_user_id, content
  65. )
  66. return replyMsg.send()
  67. else:
  68. logger.info("暂且不处理")
  69. return "success"
  70. except Exception as exc:
  71. logger.exception(exc)
  72. return exc