Receive.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import re
  2. import socket
  3. import json
  4. from Private import private_msg
  5. from SendMessage import send_message
  6. import schedule
  7. import time
  8. ListenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. ListenSocket.bind(('127.0.0.1', 5701))
  10. ListenSocket.listen(100)
  11. HttpResponseHeader = '''HTTP/1.1 200 OK
  12. Content-Type: text/html
  13. '''
  14. def daily_work():
  15. schedule.every().day.at("12:30").do(send_message,'private',2945340446, '樱花FRP签到:https://www.natfrp.com/user/')
  16. # schedule.every(10).seconds.do(send_message,'private',2945340446,'test')
  17. def request_to_json(msg):
  18. for i in range(len(msg)):
  19. if msg[i]=="{" and msg[-1]=="\n":
  20. return json.loads(msg[i:])
  21. return None
  22. #需要循环执行,返回值为json格式
  23. def rev_msg():# json or None
  24. Client, Address = ListenSocket.accept()
  25. Request = Client.recv(1024).decode(encoding='utf-8')
  26. rev_json=request_to_json(Request)
  27. Client.sendall((HttpResponseHeader).encode(encoding='utf-8'))
  28. Client.close()
  29. return rev_json
  30. def const_rev():
  31. print("start to receive!")
  32. msg_id_lst = [0 for i in range(51)]
  33. pointer = 1
  34. daily_work()
  35. while 1:
  36. rev = rev_msg()
  37. schedule.run_pending()
  38. # print(rev)
  39. if rev["post_type"] == "message" and (rev['message_id'] not in msg_id_lst):
  40. msg_id_lst[pointer] = rev['message_id']
  41. pointer = (pointer + 1) % 50
  42. if rev["message_type"] == "private": # 私聊
  43. private_msg(rev)
  44. elif rev["message_type"] == "group": # 群聊
  45. group = rev['group_id']
  46. qq = rev['sender']['user_id']
  47. print(type(group))
  48. if "[CQ:at,qq=2962298249]" in rev["raw_message"]:
  49. if rev['raw_message'].split(' ')[1] == '在吗':
  50. send_message('group', group, '[CQ:poke,qq={}]'.format(qq))
  51. else:
  52. send_message('group', group, '[CQ:at,qq={}]???咋了'.format(qq))
  53. else:
  54. continue
  55. else: # rev["post_type"]=="meta_event":
  56. continue