main.py 2.4 KB

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