msg_send_save.py 2.7 KB

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