msg_send_save.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. response = requests.post('http://127.0.0.1:3000/send_group_msg', json={
  37. 'group_id': group_id,
  38. 'message': [{
  39. "type": "at",
  40. "data": {
  41. "qq": f"{user_id}"
  42. }
  43. },{
  44. 'type': 'image',
  45. 'data': {
  46. 'file': image_path
  47. }
  48. }]
  49. })
  50. return response.json()
  51. def send_group_poke(group_id, user_id):
  52. """
  53. 发送群聊戳一戳
  54. """
  55. requests.post('http://127.0.0.1:3000/group_poke', json={
  56. 'group_id': group_id,
  57. 'user_id':user_id
  58. })
  59. def save_group_message(group_id, user_name, raw_message):
  60. """
  61. 保存聊天记录,按群名创建文件夹
  62. """
  63. now = datetime.datetime.now()
  64. year = now.year
  65. month = now.month
  66. day = now.day
  67. hour = now.hour
  68. minute = now.minute
  69. sec = now.second
  70. now_time = f"{year}_{month}_{day}_{hour}_{minute}_{sec}"
  71. # 构建群号文件夹路径,如果文件夹不存在则创建它
  72. folder_path = '群聊记录'
  73. if not os.path.exists(folder_path):
  74. os.mkdir(folder_path)
  75. # 构建文件路径,将群号作为文件名的一部分,并加上.csv后缀
  76. file_path = os.path.join(folder_path, str(group_id) + '.csv')
  77. if not os.path.exists(file_path):
  78. with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
  79. writer = csv.writer(csvfile)
  80. writer.writerow(["group_id", "user_name", "message", "time_now"])
  81. with open(file_path, 'a', newline='', encoding='utf-8') as csvfile:
  82. writer = csv.writer(csvfile)
  83. writer.writerow([group_id, user_name, raw_message, now_time])
  84. def del_group_message(message_id):
  85. """
  86. 撤回消息,message_id为发送消息的返回响应中获取
  87. """
  88. requests.post('http://127.0.0.1:3000//delete_msg', json={
  89. 'message_id': str(message_id)
  90. })
  91. def send_feishu(message):
  92. """
  93. 用来发送飞书消息
  94. """
  95. headers = {
  96. 'Content-Type': 'application/json',
  97. 'Authorization': key_get("feishu")
  98. }
  99. requests.post('https://api.gxx12138.space/feishu/text', headers=headers, json={
  100. 'content': message
  101. })
  102. if __name__ == '__main__':
  103. send_feishu('测试')