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.

common.py 2.2KB

1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1 vuosi sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import hashlib
  2. import textwrap
  3. from config import conf
  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(
  32. f"""\
  33. 感谢您的关注!
  34. 这里是ChatGPT,可以自由对话。
  35. 资源有限,回复较慢,请勿着急。
  36. 支持通用表情输入。
  37. 暂时不支持图片输入。
  38. 支持图片输出,画字开头的问题将回复图片链接。
  39. 支持角色扮演和文字冒险两种定制模式对话。
  40. 输入'{trigger_prefix}#帮助' 查看详细指令。"""
  41. )
  42. return msg
  43. def split_string_by_utf8_length(string, max_length, max_split=0):
  44. encoded = string.encode("utf-8")
  45. start, end = 0, 0
  46. result = []
  47. while end < len(encoded):
  48. if max_split > 0 and len(result) >= max_split:
  49. result.append(encoded[start:].decode("utf-8"))
  50. break
  51. end = start + max_length
  52. # 如果当前字节不是 UTF-8 编码的开始字节,则向前查找直到找到开始字节为止
  53. while end < len(encoded) and (encoded[end] & 0b11000000) == 0b10000000:
  54. end -= 1
  55. result.append(encoded[start:end].decode("utf-8"))
  56. start = end
  57. return result