You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.0KB

  1. import textwrap
  2. import web
  3. from config import conf
  4. from wechatpy.utils import check_signature
  5. from wechatpy.crypto import WeChatCrypto
  6. from wechatpy.exceptions import InvalidSignatureException
  7. MAX_UTF8_LEN = 2048
  8. class WeChatAPIException(Exception):
  9. pass
  10. def verify_server(data):
  11. try:
  12. signature = data.signature
  13. timestamp = data.timestamp
  14. nonce = data.nonce
  15. echostr = data.get("echostr", None)
  16. token = conf().get("wechatmp_token") # 请按照公众平台官网\基本配置中信息填写
  17. check_signature(token, signature, timestamp, nonce)
  18. return echostr
  19. except InvalidSignatureException:
  20. raise web.Forbidden("Invalid signature")
  21. except Exception as e:
  22. raise web.Forbidden(str(e))
  23. def subscribe_msg():
  24. trigger_prefix = conf().get("single_chat_prefix", [""])[0]
  25. msg = textwrap.dedent(
  26. f"""\
  27. 感谢您的关注!
  28. 这里是ChatGPT,可以自由对话。
  29. 资源有限,回复较慢,请勿着急。
  30. 支持语音对话。
  31. 支持图片输入。
  32. 支持图片输出,画字开头的消息将按要求创作图片。
  33. 支持tool、角色扮演和文字冒险等丰富的插件。
  34. 输入'{trigger_prefix}#帮助' 查看详细指令。"""
  35. )
  36. return msg
  37. def split_string_by_utf8_length(string, max_length, max_split=0):
  38. encoded = string.encode("utf-8")
  39. start, end = 0, 0
  40. result = []
  41. while end < len(encoded):
  42. if max_split > 0 and len(result) >= max_split:
  43. result.append(encoded[start:].decode("utf-8"))
  44. break
  45. end = min(start + max_length, len(encoded))
  46. # 如果当前字节不是 UTF-8 编码的开始字节,则向前查找直到找到开始字节为止
  47. while end < len(encoded) and (encoded[end] & 0b11000000) == 0b10000000:
  48. end -= 1
  49. result.append(encoded[start:end].decode("utf-8"))
  50. start = end
  51. return result