msg_send_save.py 3.6 KB

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