msg_send_save.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import csv
  2. import datetime
  3. import os
  4. import requests
  5. from config import key_get
  6. def send_private_message_word(user_id,word):
  7. """
  8. 用来回复私人消息
  9. """
  10. requests.post('http://127.0.0.1:3000/send_private_msg', json={
  11. 'user_id': user_id,
  12. 'message': [{
  13. 'type': 'text',
  14. 'data': {
  15. 'text': word
  16. }
  17. }]
  18. })
  19. def send_group_message_word(group_id, word):
  20. """
  21. 用来给特定群聊发送文字
  22. """
  23. requests.post('http://127.0.0.1:3000/send_group_msg', json={
  24. 'group_id': group_id,
  25. 'message': [{
  26. 'type': 'text',
  27. 'data': {
  28. 'text': word
  29. }
  30. }]
  31. })
  32. def send_group_message_pic(group_id, user_id, image_path):
  33. """
  34. 给特定群聊的特定人发送图片, image_path为图片url,可以是本地文件也可以是网络地址
  35. """
  36. file_path = "http://host.docker.internal:3100/pic" + f"?path=/{image_path}"
  37. response = requests.post('http://127.0.0.1:3000/send_group_msg', json={
  38. 'group_id': group_id,
  39. 'message': [{
  40. "type": "at",
  41. "data": {
  42. "qq": f"{user_id}"
  43. }
  44. },{
  45. 'type': 'image',
  46. 'data': {
  47. 'file': file_path
  48. }
  49. }]
  50. })
  51. return response.json()
  52. def send_group_poke(group_id, user_id):
  53. """
  54. 发送群聊戳一戳
  55. """
  56. requests.post('http://127.0.0.1:3000/group_poke', json={
  57. 'group_id': group_id,
  58. 'user_id':user_id
  59. })
  60. def save_group_message(group_id, user_name, raw_message):
  61. """
  62. 保存聊天记录,按群名创建文件夹
  63. """
  64. now = datetime.datetime.now()
  65. year = now.year
  66. month = now.month
  67. day = now.day
  68. hour = now.hour
  69. minute = now.minute
  70. sec = now.second
  71. now_time = f"{year}_{month}_{day}_{hour}_{minute}_{sec}"
  72. # 构建群号文件夹路径,如果文件夹不存在则创建它
  73. folder_path = '群聊记录'
  74. if not os.path.exists(folder_path):
  75. os.mkdir(folder_path)
  76. # 构建文件路径,将群号作为文件名的一部分,并加上.csv后缀
  77. file_path = os.path.join(folder_path, str(group_id) + '.csv')
  78. if not os.path.exists(file_path):
  79. with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
  80. writer = csv.writer(csvfile)
  81. writer.writerow(["group_id", "user_name", "message", "time_now"])
  82. with open(file_path, 'a', newline='', encoding='utf-8') as csvfile:
  83. writer = csv.writer(csvfile)
  84. writer.writerow([group_id, user_name, raw_message, now_time])
  85. def del_group_message(message_id):
  86. """
  87. 撤回消息,message_id为发送消息的返回响应中获取
  88. """
  89. requests.post('http://127.0.0.1:3000//delete_msg', json={
  90. 'message_id': str(message_id)
  91. })
  92. def send_feishu(message):
  93. """
  94. 用来发送飞书消息
  95. """
  96. headers = {
  97. 'Content-Type': 'application/json',
  98. 'Authorization': key_get("feishu")
  99. }
  100. requests.post('https://api.gxx12138.space/feishu/text', headers=headers, json={
  101. 'content': message
  102. })
  103. if __name__ == '__main__':
  104. send_feishu('测试')