1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import csv
- import datetime
- import os
- import requests
- def send_private_message_word(user_id,word):
- """
- 用来回复私人消息
- """
- requests.post('http://127.0.0.1:3000/send_private_msg', json={
- 'user_id': user_id,
- 'message': [{
- 'type': 'text',
- 'data': {
- 'text': word
- }
- }]
- })
- def send_group_message_word(group_id, word):
- """
- 用来给特定群聊发送文字
- """
- requests.post('http://127.0.0.1:3000/send_group_msg', json={
- 'group_id': group_id,
- 'message': [{
- 'type': 'text',
- 'data': {
- 'text': word
- }
- }]
- })
- def send_group_message_pic(group_id, user_id, image_path):
- """
- 给特定群聊的特定人发送图片, image_path为图片url,可以是本地文件也可以是网络地址
- """
- response = requests.post('http://127.0.0.1:3000/send_group_msg', json={
- 'group_id': group_id,
- 'message': [{
- "type": "at",
- "data": {
- "qq": f"{user_id}"
- }
- },{
- 'type': 'image',
- 'data': {
- 'file': image_path
- }
- }]
- })
- return response.json()
- def send_group_poke(group_id, user_id):
- """
- 发送群聊戳一戳
- """
- requests.post('http://127.0.0.1:3000/group_poke', json={
- 'group_id': group_id,
- 'user_id':user_id
- })
- def save_group_message(group_id, user_name, raw_message):
- """
- 保存聊天记录,按群名创建文件夹
- """
- now = datetime.datetime.now()
- year = now.year
- month = now.month
- day = now.day
- hour = now.hour
- minute = now.minute
- sec = now.second
- now_time = f"{year}_{month}_{day}_{hour}_{minute}_{sec}"
- # 构建群号文件夹路径,如果文件夹不存在则创建它
- folder_path = '群聊记录'
- if not os.path.exists(folder_path):
- os.mkdir(folder_path)
- # 构建文件路径,将群号作为文件名的一部分,并加上.csv后缀
- file_path = os.path.join(folder_path, str(group_id) + '.csv')
- if not os.path.exists(file_path):
- with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
- writer = csv.writer(csvfile)
- writer.writerow(["group_id", "user_name", "message", "time_now"])
- with open(file_path, 'a', newline='', encoding='utf-8') as csvfile:
- writer = csv.writer(csvfile)
- writer.writerow([group_id, user_name, raw_message, now_time])
- def del_group_message(message_id):
- """
- 撤回消息,message_id为发送消息的返回响应中获取
- """
- requests.post('http://127.0.0.1:3000//delete_msg', json={
- 'message_id': str(message_id)
- })
|