您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wechat_channel.py 924B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """
  2. wechat channel
  3. """
  4. import itchat
  5. import time
  6. import random
  7. import json
  8. from itchat.content import *
  9. from channel.channel import Channel
  10. @itchat.msg_register([TEXT])
  11. def handler_receive_msg(msg):
  12. WechatChannel().handle(msg)
  13. class WechatChannel(Channel):
  14. def __init__(self):
  15. pass
  16. def startup(self):
  17. # login by scan QRCode
  18. itchat.auto_login()
  19. # start message listener
  20. itchat.run()
  21. def handle(self, msg):
  22. print("handle: ", msg)
  23. print(json.dumps(msg, ensure_ascii=False))
  24. from_user_id = msg['FromUserName']
  25. other_user_id = msg['User']['UserName']
  26. if from_user_id == other_user_id:
  27. self.send(super().build_reply_content(msg['Text']), from_user_id)
  28. def send(self, msg, receiver):
  29. time.sleep(random.randint(1, 3))
  30. print(msg, receiver)
  31. itchat.send(msg + " [bot]", toUserName=receiver)