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.

63 satır
2.2KB

  1. from config import conf
  2. import hashlib
  3. import textwrap
  4. MAX_UTF8_LEN = 2048
  5. class WeChatAPIException(Exception):
  6. pass
  7. def verify_server(data):
  8. try:
  9. if len(data) == 0:
  10. return "None"
  11. signature = data.signature
  12. timestamp = data.timestamp
  13. nonce = data.nonce
  14. echostr = data.echostr
  15. token = conf().get('wechatmp_token') #请按照公众平台官网\基本配置中信息填写
  16. data_list = [token, timestamp, nonce]
  17. data_list.sort()
  18. sha1 = hashlib.sha1()
  19. # map(sha1.update, data_list) #python2
  20. sha1.update("".join(data_list).encode('utf-8'))
  21. hashcode = sha1.hexdigest()
  22. print("handle/GET func: hashcode, signature: ", hashcode, signature)
  23. if hashcode == signature:
  24. return echostr
  25. else:
  26. return ""
  27. except Exception as Argument:
  28. return Argument
  29. def subscribe_msg():
  30. trigger_prefix = conf().get('single_chat_prefix',[''])[0]
  31. msg = textwrap.dedent(f"""\
  32. 感谢您的关注!
  33. 这里是ChatGPT,可以自由对话。
  34. 资源有限,回复较慢,请勿着急。
  35. 支持通用表情输入。
  36. 暂时不支持图片输入。
  37. 支持图片输出,画字开头的问题将回复图片链接。
  38. 支持角色扮演和文字冒险两种定制模式对话。
  39. 输入'{trigger_prefix}#帮助' 查看详细指令。""")
  40. return msg
  41. def split_string_by_utf8_length(string, max_length, max_split=0):
  42. encoded = string.encode('utf-8')
  43. start, end = 0, 0
  44. result = []
  45. while end < len(encoded):
  46. if max_split > 0 and len(result) >= max_split:
  47. result.append(encoded[start:].decode('utf-8'))
  48. break
  49. end = start + max_length
  50. # 如果当前字节不是 UTF-8 编码的开始字节,则向前查找直到找到开始字节为止
  51. while end < len(encoded) and (encoded[end] & 0b11000000) == 0b10000000:
  52. end -= 1
  53. result.append(encoded[start:end].decode('utf-8'))
  54. start = end
  55. return result