JS爬虫实战演练

在这个小红书私信通里面进行一个js的爬虫

文字发送

async function sendChatMessage(content) {const url = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/chatline/msg';const params = new URLSearchParams({porch_user_id: '677e116404ee000000000001'});const messageData = {sender_porch_id: "677e116404ee000000000001",receiver_id: "612368ee000000000101cd0c",content: content,message_type: "TEXT",platform:3,uuid: uuid()};const headers = {'accept': 'application/json, text/plain, */*','accept-encoding': 'gzip, deflate, br, zstd','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw',  'content-type': 'application/json','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-subsystem': 'ares'};try {console.log('发送数据:', messageData);const response = await fetch(`${url}?${params}`, {method: 'POST',headers: headers,credentials: 'include',body: JSON.stringify(messageData)});if (!response.ok) {const errorText = await response.text();console.error('错误详情:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const result = await response.json();console.log('发送成功:', result);return result;} catch (error) {console.error('发送失败:', error);throw error;}
}// 测试发送
async function testSend() {try {console.log('当前 cookie:', document.cookie);const result = await sendChatMessage("test message");console.log('发送结果:', result);} catch (error) {console.error('发送出错:', error);}
}
// 执行测试
testSend();

图片发送

//第一步进行token获取
async function getUploadTokenData() {// 构建URL和参数const baseUrl = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/uploader/v3/token';const params = new URLSearchParams({biz_name: 'cs',scene: 'feeva_img',file_count: '1',version: '1',source: 'web'});try {const response = await fetch(`${baseUrl}?${params}`, {method: 'GET',headers: {'accept': 'application/json, text/plain, */*','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','x-b3-traceid': 'dba620e6f7ba1f67','x-subsystem': 'ares','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'},credentials: 'include'  // 包含cookies});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();// 检查响应是否成功并返回data部分if (data.success && data.code === 0) {return {success: true,data: data.data};} else {return {success: false,error: data.msg || '获取数据失败'};}} catch (error) {console.error('Error fetching upload token:', error);return {success: false,error: error.message};}
}// 使用示例
async function example() {try {const result = await getUploadTokenData();if (result.success) {console.log('Upload permits:', result.data.upload_temp_permits);console.log('Result:', result.data.result);// 获取具体的上传许可信息const permit = result.data.upload_temp_permits[0];console.log('File ID:', permit.file_ids[0]);console.log('Upload Token:', permit.token);console.log('Upload Address:', permit.upload_addr);console.log('Expire Time:', new Date(permit.expire_time));} else {console.error('Failed to get data:', result.error);}} catch (error) {console.error('Error:', error);}
}// 执行示例
example();//第二步put上传图片
async function uploadImageUrl(imageUrl) {// 1. 首先获取图片数据const imageResponse = await fetch(imageUrl);const imageBlob = await imageResponse.blob();// 2. 准备上传请求const uploadUrl = 'https://ros-upload.xiaohongshu.com/rimmatrix/V6nuam8zyZhLcjZdJfl1HEkhCXSx3GHlj2os4CEpSlVYbzo';try {const response = await fetch(uploadUrl, {method: 'PUT',headers: {'accept': '*/*','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'q-sign-algorithm=sha1&q-ak=null&q-sign-time=1736479724;1736566124&q-key-time=1736479724;1736566124&q-header-list=content-length;host&q-url-param-list=&q-signature=18d7f7bce19414d500bf7bf33b2b027bc8fecc88','content-type': 'image/jpeg','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/','x-cos-security-token': 'iFbRMAXXIamH6mFfYAecIk6sRug:eyJkZWFkbGluZSI6MTczNjU3MDQ0OSwiYWxsb3dQcmVmaXhlcyI6WyJWNm51YW04enlaaExjalpkSmZsMUhFa2hDWFN4M0dIbGoyb3M0Q0VwU2xWWWJ6byJdfQ','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-site'},body: imageBlob});if (response.ok) {const previewUrl = response.headers.get('x-ros-preview-url');return {success: true,previewUrl: previewUrl,etag: response.headers.get('etag')};} else {throw new Error(`Upload failed with status: ${response.status}`);}} catch (error) {console.error('Error uploading image:', error);return {success: false,error: error.message};}
}// 使用示例:
const imageUrl = 'https://filecenter.kyliao.com:89/KYL/1/wxid_2a1a5t10214712/2025-01-09/3a71fa92-dce7-475f-ae44-ed62c79f7b5d.png'; // 替换为您的图片URL
uploadImageUrl(imageUrl).then(result => {if (result.success) {console.log('Upload successful!');console.log('Preview URL:', result.previewUrl);console.log('ETag:', result.etag);} else {console.error('Upload failed:', result.error);}}).catch(error => {console.error('Error:', error);});(0,De.Ix)//最后一步 msg发送
async function testSend() {try {// 先构造图片数据对象const imageData = {link: {cloudType: 4,bizName: "cs",scene: "feeva_img",fileId: "rimmatrix/RWuNee2PEHOq7_jYoFamLUouQZeS2uDXIbpwnuZr1F2CudM",preViewUrl: "https://ros-preview.xhscdn.com/rimmatrix/V6nuam8zyZhLcjZdJfl1HEkhCXSx3GHlj2os4CEpSlVYbzo?sign=ae5c1f2b90482122bd9262a2cfa12da6&t=1736485284"},size: {width: 337,height: 170}};console.log('当前 cookie:', document.cookie);// 发送消息时,需要修改 message_type 为 "IMAGE"const messageData = {sender_porch_id: "677e116404ee000000000001",receiver_id: "612368ee000000000101cd0c",content: JSON.stringify(imageData),  // 将图片数据转换为字符串message_type: "IMAGE",  // 改为 IMAGE 类型platform: 3,uuid: uuid()};const result = await sendChatMessage(messageData);console.log('发送结果:', result);} catch (error) {console.error('发送出错:', error);}
}// 修改 sendChatMessage 函数,直接接收消息数据对象
async function sendChatMessage(messageData) {const url = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/chatline/msg';const params = new URLSearchParams({porch_user_id: '677e116404ee000000000001'});const headers = {'accept': 'application/json, text/plain, */*','accept-encoding': 'gzip, deflate, br, zstd','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','content-type': 'application/json','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-subsystem': 'ares'};try {console.log('发送数据:', messageData);const response = await fetch(`${url}?${params}`, {method: 'POST',headers: headers,credentials: 'include',body: JSON.stringify(messageData)});if (!response.ok) {const errorText = await response.text();console.error('错误详情:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const result = await response.json();console.log('发送成功:', result);return result;} catch (error) {console.error('发送失败:', error);throw error;}
}
// 执行测试
testSend();

回撤功能

//先发送消息 然后获取id
async function sendPostRequest() {const url = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/chatline/msg?porch_user_id=677e116404ee000000000001';const payload = {sender_porch_id: "677e116404ee000000000001",receiver_id: "612368ee000000000101cd0c",content: "ccccc",message_type: "TEXT",platform: 3,uuid: "1736478957090-43350413"};const headers = {'accept': 'application/json, text/plain, */*','accept-encoding': 'gzip, deflate, br, zstd','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','content-type': 'application/json','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-subsystem': 'ares','cookie': 'abRequestId=f2154e9b-797b-5124-baaa-c2afb64b6f79; a1=194491d8b8fa14itukoamsqt4nf4p629kv1nvnb1250000382687; webId=0a7ab39304a5a8d090bd6ddcc8848598; web_session=030037a04c168270c118ba0407204a3a7c64a9; gid=yj44jyfj08Tfyj44jyfYDhIDYi0y4397Tl06yyJMA94IiI284UKJjh888qYJKYW8SjS84q4j; customerClientId=621527812963092; customer-sso-sid=68c5174577616452305268601fca10ccf489f9fe; x-user-id-pro.xiaohongshu.com=63a188ad0000000026013607; access-token-pro.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxwq0enw; access-token-pro.beta.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxwq0enw; xsecappid=pro-base; acw_tc=0a0d0e0317364784102826015e342af72d01e74806b984ca4f111406df3b0f; websectiga=10f9a40ba454a07755a08f27ef8194c53637eba4551cf9751c009d9afb564467; sec_poison_id=611c6572-b0c6-43fc-a7e7-dd15e153861d'};try {const response = await fetch(url, {method: 'POST',headers: headers,body: JSON.stringify(payload)});if (!response.ok) {const errorText = await response.text();console.error('请求失败:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const result = await response.json();console.log('请求成功:', result);// 提取 message_idconst messageId = result.data?.message_id;if (messageId) {console.log('Message ID:', messageId);} else {console.log('Message ID not found in response');}return result;} catch (error) {console.error('请求出错:', error);throw error;}
}// 调用函数发送请求
sendPostRequest();async function sendDeleteRequest() {const url = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/chatline/msg';const params = new URLSearchParams({porch_user_id: '677e116404ee000000000001',id: '612368ee000000000101cd0c.63a188ad0000000026013607.1e78093375f6b8a',customer_user_id: '612368ee000000000101cd0c'});const headers = {'accept': 'application/json, text/plain, */*','accept-encoding': 'gzip, deflate, br, zstd','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-subsystem': 'ares','cookie': 'abRequestId=f2154e9b-797b-5124-baaa-c2afb64b6f79; a1=194491d8b8fa14itukoamsqt4nf4p629kv1nvnb1250000382687; webId=0a7ab39304a5a8d090bd6ddcc8848598; web_session=030037a04c168270c118ba0407204a3a7c64a9; gid=yj44jyfj08Tfyj44jyfYDhIDYi0y4397Tl06yyJMA94IiI284UKJjh888qYJKYW8SjS84q4j; customerClientId=621527812963092; customer-sso-sid=68c5174577616452305268601fca10ccf489f9fe; x-user-id-pro.xiaohongshu.com=63a188ad0000000026013607; access-token-pro.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxmrxwq0enw; access-token-pro.beta.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxmrxwq0enw; xsecappid=pro-base; acw_tc=0a0d0e0317364784102826015e342af72d01e74806b984ca4f111406df3b0f; websectiga=cf46039d1971c7b9a650d87269f31ac8fe3bf71d61ebf9d9a0a87efb414b816c; sec_poison_id=b36eccf0-9bce-4f66-8da8-20cd23f25541'};try {const response = await fetch(`${url}?${params}`, {method: 'DELETE',headers: headers});if (!response.ok) {const errorText = await response.text();console.error('请求失败:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const result = await response.json();console.log('请求成功:', result);return result;} catch (error) {console.error('请求出错:', error);throw error;}
}// 调用函数发送请求
sendDeleteRequest();

获取头像和昵称

async function getChatList() {const url = 'https://pro.xiaohongshu.com/api/edith/ads/pro/chat/chatline/chat';const params = new URLSearchParams({porch_user_id: '677e116404ee000000000001',limit: '80'});const headers = {'authority': 'pro.xiaohongshu.com','accept': 'application/json, text/plain, */*','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','cookie': 'abRequestId=f2154e9b-797b-5124-baaa-c2afb64b6f79; a1=194491d8b8fa14itukoamsqt4nf4p629kv1nvnb1250000382687; webId=0a7ab39304a5a8d090bd6ddcc8848598; web_session=030037a04c168270c118ba0407204a3a7c64a9; gid=yj44jyfj08Tfyj44jyfYDhIDYi0y4397Tl06yyJMA94IiI284UKJjh888qYJKYW8SjS84q4j; customerClientId=621527812963092; customer-sso-sid=68c5174577616452305268601fca10ccf489f9fe; x-user-id-pro.xiaohongshu.com=63a188ad0000000026013607; access-token-pro.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxmrxwq0enw; access-token-pro.beta.xiaohongshu.com=customer.ares.AT-68c517457761645230526862lrx8ruxmrxwq0enw; xsecappid=pro-base; acw_tc=0a4252f017364116529762089e968dff6b8b6e47ea444b0b76026fa2d1e664; websectiga=9730ffafd96f2d09dc024760e253af6ab1feb0002827740b95a255ddf6847fc8; sec_poison_id=f5e5bad7-0c91-4002-8189-51147435d119','referer': 'https://pro.xiaohongshu.com/im/liberal','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-subsystem': 'ares'};try {const response = await fetch(`${url}?${params.toString()}`, {method: 'GET',headers: headers});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();if (data.success && data.data && data.data.chat_list) {// 格式化聊天列表数据const formattedChats = data.data.chat_list.map(chat => {const timestamp = chat.sixin_chat.last_msg_ts;const date = new Date(timestamp);return {// 用户基本信息userInfo: {userId: chat.sixin_chat.user_id,nickname: chat.sixin_chat.nickname,avatarUrl: chat.sixin_chat.avatar,avatarLink: `头像链接: ${chat.sixin_chat.avatar}` // 新增头像链接显示},// 最后一条消息信息lastMessage: {content: chat.sixin_chat.last_msg_content,timestamp: timestamp,formattedTime: date.toLocaleString(),storeId: chat.sixin_chat.last_store_id},// 会话信息sessionInfo: {sessionId: chat.session.session_id,status: chat.session.state,replied: chat.session.is_replied,grantorUserId: chat.session.grantor_user_id},// 聊天状态chatStatus: {isActive: chat.long_chat_view.active,isFavorite: chat.long_chat_view.favorite,lastActiveTime: new Date(chat.long_chat_view.last_msg_ts).toLocaleString(),isBlacklisted: chat.in_blacklist}};});// 打印格式化后的数据console.log('\n=== 聊天列表摘要 ===');formattedChats.forEach((chat, index) => {console.log(`\n【聊天 ${index + 1}】`);console.log(`用户: ${chat.userInfo.nickname}`);console.log(`ID: ${chat.userInfo.userId}`);console.log(chat.userInfo.avatarLink);  // 显示头像链接console.log(`最新消息: ${chat.lastMessage.content}`);console.log(`发送时间: ${chat.lastMessage.formattedTime}`);console.log(`会话状态: ${chat.sessionInfo.status}`);console.log(`已回复: ${chat.sessionInfo.replied ? '是' : '否'}`);console.log(`活跃状态: ${chat.chatStatus.isActive ? '活跃' : '不活跃'}`);console.log('------------------------');});// 保存完整数据到文件if (typeof require !== 'undefined') {const fs = require('fs');fs.writeFileSync('formatted_chat_list.json', JSON.stringify(formattedChats, null, 2));console.log('\n详细数据已保存到 formatted_chat_list.json');}return formattedChats;}return null;} catch (error) {console.error('获取聊天列表失败:', error.message);return null;}
}// main函数保持不变
async function main() {try {console.log('正在获取聊天列表...');const chatList = await getChatList();if (chatList) {console.log(`\n成功获取 ${chatList.length} 个聊天会话`);// 统计信息const stats = {activeChats: chatList.filter(chat => chat.chatStatus.isActive).length,repliedChats: chatList.filter(chat => chat.sessionInfo.replied).length,favoriteChats: chatList.filter(chat => chat.chatStatus.isFavorite).length};console.log('\n=== 统计信息 ===');console.log(`活跃会话: ${stats.activeChats}`);console.log(`已回复会话: ${stats.repliedChats}`);console.log(`收藏会话: ${stats.favoriteChats}`);return chatList;}} catch (error) {console.error('执行出错:', error);}
}// 运行代码
main().catch(console.error);

完整Class封裝結合代碼

class XiaohongshuAPI {constructor() {this.baseUrl = 'https://pro.xiaohongshu.com/api';this.porchUserId = '677e116404ee000000000001';this.receiverId = '612368ee000000000101cd0c';this.headers = {'accept': 'application/json, text/plain, */*','accept-encoding': 'gzip, deflate, br, zstd','accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','content-type': 'application/json','origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0','x-b3-traceid': 'c46a27c6cb6be409','x-subsystem': 'ares','cookie': document.cookie // 确保在浏览器环境中使用};}// 工具方法: 生成 UUID(需要实现或使用现有库)generateUUID() {// 简单的UUID生成方法(不保证唯一性,推荐使用库如 'uuid')return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);return v.toString(16);});}// 查看用户登录状态async fetchUserInfo() {const url = `${this.baseUrl}/eros/user/info`;try {const response = await fetch(url, {method: 'GET',headers: this.headers,credentials: 'include'});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const result = await response.json();// 检查是否有 data 字段if (result.data) {console.log('用户信息:', result.data);return result.data;} else {console.log('响应中未找到 data 字段');return null;}} catch (error) {console.error('获取用户信息时出错:', error);return null;}}// 发送消息async sendChatMessage(messageData) {const url = `${this.baseUrl}/edith/ads/pro/chat/chatline/msg`;const params = new URLSearchParams({porch_user_id: this.porchUserId});try {console.log('发送消息数据:', messageData);const response = await fetch(`${url}?${params}`, {method: 'POST',headers: this.headers,credentials: 'include',body: JSON.stringify(messageData)});if (!response.ok) {const errorText = await response.text();console.error('POST 请求失败:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const result = await response.json();console.log('发送成功:', result);const messageId = result.data?.message_id;if (messageId) {console.log('消息 ID:', messageId);return messageId;} else {console.log('响应中未找到消息 ID');return null;}} catch (error) {console.error('发送消息时出错:', error);return null;}}// 撤回消息async sendDeleteRequest(messageId) {const deleteUrl = `${this.baseUrl}/edith/ads/pro/chat/chatline/msg`;const params = new URLSearchParams({porch_user_id: this.porchUserId,id: messageId,customer_user_id: this.receiverId});try {const response = await fetch(`${deleteUrl}?${params}`, {method: 'DELETE',headers: this.headers});if (!response.ok) {const errorText = await response.text();console.error('DELETE 请求失败:', errorText);throw new Error(`HTTP error! status: ${response.status}, message: ${errorText}`);}const deleteResult = await response.json();console.log('DELETE 请求成功:', deleteResult);return deleteResult;} catch (error) {console.error('撤回消息时出错:', error);return null;}}// 撤回模块: 发送消息并撤回async sendPostAndDeleteRequest(content = "ccccc") {try {// Step 1: 发送消息const messageId = await this.sendChatMessage({sender_porch_id: this.porchUserId,receiver_id: this.receiverId,content: content,message_type: "TEXT",platform: 3,uuid: this.generateUUID()});if (messageId) {// Step 2: 撤回消息await this.sendDeleteRequest(messageId);}} catch (error) {console.error('发送或撤回消息时出错:', error);}}// 获取聊天列表async getChatList() {const url = `${this.baseUrl}/edith/ads/pro/chat/chatline/chat`;const params = new URLSearchParams({porch_user_id: this.porchUserId,limit: '80'});try {const response = await fetch(`${url}?${params.toString()}`, {method: 'GET',headers: this.headers});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();if (data.success && data.data && data.data.chat_list) {const formattedChats = data.data.chat_list.map(chat => {const timestamp = chat.sixin_chat.last_msg_ts;const date = new Date(timestamp);return {userInfo: {userId: chat.sixin_chat.user_id,nickname: chat.sixin_chat.nickname,avatarUrl: chat.sixin_chat.avatar,avatarLink: `头像链接: ${chat.sixin_chat.avatar}`},lastMessage: {content: chat.sixin_chat.last_msg_content,timestamp: timestamp,formattedTime: date.toLocaleString(),storeId: chat.sixin_chat.last_store_id},sessionInfo: {sessionId: chat.session.session_id,status: chat.session.state,replied: chat.session.is_replied,grantorUserId: chat.session.grantor_user_id},chatStatus: {isActive: chat.long_chat_view.active,isFavorite: chat.long_chat_view.favorite,lastActiveTime: new Date(chat.long_chat_view.last_msg_ts).toLocaleString(),isBlacklisted: chat.in_blacklist}};});console.log('\n=== 聊天列表摘要 ===');formattedChats.forEach((chat, index) => {console.log(`\n【聊天 ${index + 1}】`);console.log(`用户: ${chat.userInfo.nickname}`);console.log(`ID: ${chat.userInfo.userId}`);console.log(chat.userInfo.avatarLink);console.log(`最新消息: ${chat.lastMessage.content}`);console.log(`发送时间: ${chat.lastMessage.formattedTime}`);console.log(`会话状态: ${chat.sessionInfo.status}`);console.log(`已回复: ${chat.sessionInfo.replied ? '是' : '否'}`);console.log(`活跃状态: ${chat.chatStatus.isActive ? '活跃' : '不活跃'}`);console.log('------------------------');});// 保存完整数据到文件if (typeof require !== 'undefined') {const fs = require('fs');fs.writeFileSync('formatted_chat_list.json',JSON.stringify(formattedChats, null, 2));console.log('\n详细数据已保存到 formatted_chat_list.json');}return formattedChats;}console.log('响应数据不符合预期');return null;} catch (error) {console.error('获取聊天列表失败:', error.message);return null;}}// 获取上传令牌数据async getUploadTokenData() {const url = `${this.baseUrl}/edith/ads/pro/chat/uploader/v3/token`;const params = new URLSearchParams({biz_name: 'cs',scene: 'feeva_img',file_count: '1',version: '1',source: 'web'});try {const response = await fetch(`${url}?${params}`, {method: 'GET',headers: {'accept': 'application/json, text/plain, */*','authorization': 'AT-68c517457761645230526862lrx8ruxmrxwq0enw','x-b3-traceid': 'dba620e6f7ba1f67','x-subsystem': 'ares','referer': 'https://pro.xiaohongshu.com/im/multiCustomerService','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0'},credentials: 'include'});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();if (data.success && data.code === 0) {return data.data.upload_temp_permits[0];} else {throw new Error(data.msg || '获取上传令牌数据失败');}} catch (error) {console.error('获取上传令牌时出错:', error);throw error;}}// 上传图片async uploadImage(imageUrl, permit) {try {const imageResponse = await fetch(imageUrl);const imageBlob = await imageResponse.blob();console.log('上传令牌:', permit.token);console.log('上传地址:', permit.upload_addr);console.log('文件ID:', permit.file_ids[0]);const uploadUrl = `https://ros-upload.xiaohongshu.com/${permit.file_ids[0]}`;const response = await fetch(uploadUrl, {method: 'PUT',headers: {'accept': '*/*','content-type': 'image/jpeg','authorization': permit.token,'origin': 'https://pro.xiaohongshu.com','referer': 'https://pro.xiaohongshu.com/','x-cos-security-token': permit.token,'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"','sec-ch-ua-mobile': '?0','sec-ch-ua-platform': '"Windows"','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-site'},body: imageBlob});if (response.ok) {const previewUrl = response.headers.get('x-ros-preview-url');console.log('图片上传成功,预览URL:', previewUrl);return {success: true,fileId: permit.file_ids[0],previewUrl: previewUrl};} else {throw new Error(`图片上传失败,状态码: ${response.status}`);}} catch (error) {console.error('上传图片时出错:', error);throw error;}}// 执行完整流程: 获取上传令牌 -> 上传图片 -> 发送图片消息async executeWorkflow(imageUrl) {try {// Step 1: 获取上传令牌const permit = await this.getUploadTokenData();console.log('获取到的上传许可:', permit);// Step 2: 上传图片const uploadResult = await this.uploadImage(imageUrl, permit);if (!uploadResult.success) {throw new Error('图片上传失败');}// Step 3: 发送图片消息const imageData = {link: {cloudType: 4,bizName: "cs",scene: "feeva_img",fileId: uploadResult.fileId,preViewUrl: uploadResult.previewUrl},size: {width: 337,height: 170}};const messageData = {sender_porch_id: this.porchUserId,receiver_id: this.receiverId,content: JSON.stringify(imageData),message_type: "IMAGE",platform: 3,uuid: this.generateUUID()};const sendResult = await this.sendChatMessage(messageData);console.log('图片消息发送结果:', sendResult);} catch (error) {console.error('执行完整流程时出错:', error);}}// 获取未回复的聊天数据async getChatData() {const url = `${this.baseUrl}/edith/ads/pro/chat/chatline/chat`;const params = new URLSearchParams({porch_user_id: this.porchUserId,limit: '80',is_active: 'true'});try {const response = await fetch(`${url}?${params.toString()}`, {method: 'GET',headers: this.headers});if (!response.ok) {throw new Error(`HTTP error! status: ${response.status}`);}const data = await response.json();const unrepliedChats = data.data.chat_list.filter(chat =>chat.session.is_replied === false);// 打印未回复的消息console.log('\n=== 未回复的消息 ===');unrepliedChats.forEach((chat, index) => {console.log(`\n--- 消息 ${index + 1} ---`);console.log('用户:', chat.sixin_chat.nickname);console.log('用户ID:', chat.sixin_chat.user_id);console.log('最后消息:', chat.sixin_chat.last_msg_content);console.log('发送时间:', new Date(chat.sixin_chat.last_msg_ts).toLocaleString());console.log('会话ID:', chat.session.session_id);console.log('会话状态:', chat.session.state);});// 如果在Node.js环境中,保存到文件if (typeof require !== 'undefined') {const fs = require('fs');fs.writeFileSync('full_response.json',JSON.stringify(data, null, 2));console.log('\n数据已保存到 full_response.json');}return data;} catch (error) {console.error('获取数据失败:', error.message);return null;}}
}// 使用示例
const api = new XiaohongshuAPI();// 查看用户登录状态
api.fetchUserInfo();// 发送并撤回消息
api.sendPostAndDeleteRequest(id,contactIMID);// 获取聊天列表
api.getChatList();// 执行上传并发送图片的完整流程
const imageUrl = 'https://filecenter.kyliao.com:89/KYL/1/wxid_2a1a5t10214712/2025-01-09/3a71fa92-dce7-475f-ae44-ed62c79f7b5d.png'; // 替换为您的图片URL
api.executeWorkflow(imageUrl);// 测试发送文本消息
// 测试发送文本消息
api.sendChatMessage({sender_porch_id: api.porchUserId,receiver_id: api.receiverId,content: "test message",message_type: "TEXT", // 确保包含 message_typeplatform: 3,uuid: api.generateUUID()
});// 获取未回复的聊天数据
api.getChatData();

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/124.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IDEA中创建maven项目

1. IDEA中创建maven项目 在IDEA中创建Maven项目,前提是已经安装配置好Maven环境。如还未配置安装Maven的,请先下载安装。如何下载安装,可参考我另外篇文章:maven的下载与安装教程本篇教程是以创建基于servlet的JavaWeb项目为例子&…

【C++入门】详解(中)

目录 💕1.函数的重载 💕2.引用的定义 💕3.引用的一些常见问题 💕4.引用——权限的放大/缩小/平移 💕5. 不存在的空引用 💕6.引用作为函数参数的速度之快(代码体现) &#x1f4…

django基于Python的电影推荐系统

Django 基于 Python 的电影推荐系统 一、系统概述 Django 基于 Python 的电影推荐系统是一款利用 Django 框架开发的智能化应用程序,旨在为电影爱好者提供个性化的电影推荐服务。该系统通过收集和分析用户的观影历史、评分数据、电影的属性信息(如类型…

2024AAAI SCTNet论文阅读笔记

文章目录 SCTNet: Single-Branch CNN with Transformer Semantic Information for Real-Time Segmentation摘要背景创新点方法Conv-Former Block卷积注意力机制前馈网络FFN 语义信息对齐模块主干特征对齐共享解码头对齐 总体架构backbone解码器头 对齐损失 实验SOTA效果对比Cit…

_STM32关于CPU超频的参考_HAL

MCU: STM32F407VET6 官方最高稳定频率:168MHz 工具:STM32CubeMX 本篇仅仅只是提供超频(默认指的是主频)的简单方法,并未涉及STM32超频极限等问题。原理很简单,通过设置锁相环的倍频系数达到不同的频率&am…

python类和对象

一、什么是类和对象 类和对象一般是编程中较早接触到的比较抽象的概念,其实我们只要按照我们现实生活的实例去类比,就很好理解了 概念理解 我们可以把类比做是一个盖房子的图纸,对象比做是根据图纸去创建出来的一栋房子,这样每…

太原理工大学软件设计与体系结构 --javaEE

这个是简答题的内容 选择题的一些老师会给你们题库,一些注意的点我会做出文档在这个网址 项目目录预览 - TYUT复习资料:复习资料 - GitCode 希望大家可以给我一些打赏 什么是Spring的IOC和DI IOC 是一种设计思想,它将对象的创建和对象之间的依赖关系…

CNN Test Data

由于数据量过大,打不开了 搞一组小的吧。收工睡觉 https://download.csdn.net/download/spencer_tseng/90256048

Windows下调试Dify相关组件(2)--后端Api

1.部署依赖的服务(代码最外层的docker目录) 1.1 将middleware.env.example复制,并改名为middleware.env。 1.2 查看docker-compose.middleware.yaml,有5个服务 db:postgres数据库。 redis:redis缓存。 sa…

从预训练的BERT中提取Embedding

文章目录 背景前置准备思路利用Transformer 库实现 背景 假设要执行一项情感分析任务,样本数据如下 可以看到几个句子及其对应的标签,其中1表示正面情绪,0表示负面情绪。我们可以利用给定的数据集训练一个分类器,对句子所表达的…

HarmonyOS鸿蒙开发 弹窗及加载中指示器HUD功能实现

HarmonyOS鸿蒙开发 弹窗及加载中指示器HUD功能实现 最近在学习鸿蒙开发过程中,阅读了官方文档,在之前做flutter时候,经常使用overlay,使用OverlayEntry加入到overlayState来做添加悬浮按钮、提示弹窗、加载中指示器、加载失败的t…

基于华为ENSP的OSPF状态机、工作过程、配置保姆级别详解(2)

本篇技术博文摘要 🌟 基于华为enspOSPF状态机、OSPF工作过程、.OSPF基本配置等保姆级别具体详解步骤;精典图示举例说明、注意点及常见报错问题所对应的解决方法 引言 📘 在这个快速发展的技术时代,与时俱进是每个IT人的必修课。我…

DeepSeek:性能强劲的开源模型

deepseek 全新系列模型 DeepSeek-V3 首个版本上线并同步开源。登录官网 chat.deepseek.com 即可与最新版 V3 模型对话。 性能对齐海外领军闭源模型​ DeepSeek-V3 为自研 MoE 模型,671B 参数,激活 37B,在 14.8T token 上进行了预训练。 论…

Elastic-Job相关

文档参考视频:09_SpringBoot案例演示_哔哩哔哩_bilibili 一、Elastic-Job介绍 Elastic-Job 是一个轻量级、分布式的任务调度框架,旨在解决分布式环境下的定时任务调度问题。 1.1. Elastic-Job 的核心组件 Elastic-Job 是由多个核心组件构成的&#x…

【Linux】文件 文件描述符fd

🌻个人主页:路飞雪吖~ 🌠专栏:Linux 目录 🌻个人主页:路飞雪吖~ 一、C文件接口 🌟写文件 🌠小贴士: 🌠stdin && stdout && stderr Linux下…

Java Spring Boot实现基于URL + IP访问频率限制

点击下载《Java Spring Boot实现基于URL IP访问频率限制(源代码)》 1. 引言 在现代 Web 应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段。为了保护系统资源,防止服务器过载或服务不可用,需要对接口的访问频率进行限制。本文将介绍如…

QML states和transitions的使用

一、介绍 1、states Qml states是指在Qml中定义的一组状态(States),用于管理UI元素的状态转换和属性变化。每个状态都包含一组属性值的集合,并且可以在不同的状态间进行切换。 通过定义不同的状态,可以在不同的应用场…

SpringCloud

1.认识微服务 随着互联网行业的发展,对服务的要求也越来越高,服务架构也从单体架构逐渐演变为现在流行的微服务架构。这些架构之间有怎样的差别呢? 1.0.学习目标 了解微服务架构的优缺点 1.1.单体架构 单体架构:将业务的所有功…

DSP+Simulink——点亮LED灯(TMSDSP28379D)超详细

实现功能:DSP28379D-LED灯闪烁 :matlab为2019a :环境建立见之前文章 Matlab2019a安装C2000 Processors超详细过程 matlab官网链接: Getting Started with Embedded Coder Support Package for Texas Instruments C2000 Processors Overview of Creat…

java_将数据存入elasticsearch进行高效搜索

使用技术简介: (1) 使用Nginx实现反向代理,使前端可以调用多个微服务 (2) 使用nacos将多个服务管理关联起来 (3) 将数据存入elasticsearch进行高效搜索 (4) 使用消息队列rabbitmq进行消息的传递 (5) 使用 openfeign 进行多个服务之间的api调用 参…