msg_send_save.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 send_group_vioce(group_id, file_path):
  61. """
  62. 发送群语音消息
  63. """
  64. response = requests.post('http://127.0.0.1:3000/send_group_msg', json={
  65. 'group_id': group_id,
  66. 'message': [{
  67. 'type': 'record',
  68. 'data': {
  69. 'file': file_path
  70. }
  71. }]
  72. })
  73. return response.json()
  74. def save_group_message(group_id, user_name, raw_message):
  75. """
  76. 保存聊天记录,按群名创建文件夹
  77. """
  78. now = datetime.datetime.now()
  79. year = now.year
  80. month = now.month
  81. day = now.day
  82. hour = now.hour
  83. minute = now.minute
  84. sec = now.second
  85. now_time = f"{year}_{month}_{day}_{hour}_{minute}_{sec}"
  86. # 构建群号文件夹路径,如果文件夹不存在则创建它
  87. folder_path = '群聊记录'
  88. if not os.path.exists(folder_path):
  89. os.mkdir(folder_path)
  90. # 构建文件路径,将群号作为文件名的一部分,并加上.csv后缀
  91. file_path = os.path.join(folder_path, str(group_id) + '.csv')
  92. if not os.path.exists(file_path):
  93. with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
  94. writer = csv.writer(csvfile)
  95. writer.writerow(["group_id", "user_name", "message", "time_now"])
  96. with open(file_path, 'a', newline='', encoding='utf-8') as csvfile:
  97. writer = csv.writer(csvfile)
  98. writer.writerow([group_id, user_name, raw_message, now_time])
  99. def del_group_message(message_id):
  100. """
  101. 撤回消息,message_id为发送消息的返回响应中获取
  102. """
  103. requests.post('http://127.0.0.1:3000//delete_msg', json={
  104. 'message_id': str(message_id)
  105. })
  106. def send_feishu(message):
  107. """
  108. 用来发送飞书消息
  109. """
  110. headers = {
  111. 'Content-Type': 'application/json',
  112. 'Authorization': key_get("feishu")
  113. }
  114. requests.post('https://api.gxx12138.space/feishu/text', headers=headers, json={
  115. 'content': message
  116. })
  117. if __name__ == '__main__':
  118. send_feishu('测试')