add img send method

This commit is contained in:
tursom 2024-10-10 13:59:17 +08:00
parent 5685ff99cf
commit 8aa0d37ed3
4 changed files with 163 additions and 33 deletions

82
chat.js
View File

@ -1,10 +1,9 @@
client = require("./client")
const client = require("./client")
const axios = require('axios');
const http = require('http');
const {once} = require("node:events");
const { once } = require("node:events");
const server = http.createServer(async (req, res) => {
try {
async function sendMsg(req, res) {
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // 将Buffer转换为字符串
@ -14,8 +13,7 @@ const server = http.createServer(async (req, res) => {
let requests = JSON.parse(body);
console.log(requests.id)
console.log(requests.msg)
console.log(requests)
client.steamUser.chat.sendFriendMessage(requests.id, requests.msg, (err, response) => {
if (err) {
@ -30,11 +28,81 @@ const server = http.createServer(async (req, res) => {
res.setHeader('Content-Type', 'text/plain');
res.end('Success\n');
})
}
async function sendImg(req, res) {
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // 将Buffer转换为字符串
});
await once(req, 'end');
let requests = JSON.parse(body);
console.log(requests)
if (requests.url) {
img = await readUrlAsBuffer(requests.url)
} else if (requests.img) {
img = Buffer.from(requests.img, 'base64');
} else {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad Request\n');
return;
}
client.steamCommunity.sendImageToUser(requests.id, img, function (err, imageUrl) {
if (err) {
logger.error("an error occurred while sending image: ", err);
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error\n');
return
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Success\n');
});
}
const server = http.createServer(async (req, res) => {
try {
if (req.url == "/img") {
await sendImg(req, res);
} else {
await sendMsg(req, res);
}
} catch (e) {
console.error("An error occurred while processing the request: ", e);
res.statusCode = 500;
res.setHeader('Content-Type', 'text/plain');
res.end('Internal Server Error\n');
}
});
async function readUrlAsBuffer(url) {
try {
const response = await axios.get(url, { responseType: 'arraybuffer' });
return Buffer.from(response.data);
} catch (err) {
throw new Error(`Failed to fetch URL: ${err.message}`);
}
}
function readFileAsBuffer(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}
server.listen(3000, '0.0.0.0', () => {
console.log('Server running at http://0.0.0.0:3000/');
});

View File

@ -1,13 +1,14 @@
SteamID = require('steamid');
SteamUser = require('steam-user');
SteamTotp = require('steam-totp');
const SteamID = require('steamid');
const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp');
const SteamCommunity = require('steamcommunity');
fs = require('fs');
winston = require("winston");
logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: {service: 'steam-logger'},
defaultMeta: { service: 'steam-logger' },
transports: [
new winston.transports.Console(),
]
@ -16,6 +17,7 @@ logger = winston.createLogger({
const users = {};
const steamUser = new SteamUser();
const steamCommunity = new SteamCommunity();
steamUser.setOption("renewRefreshTokens", true);
steamUser.on("refreshToken", (refreshToken) => {
@ -53,8 +55,14 @@ try {
steamID: config.steamID,
});
}
steamUser.on('loggedOn', () => {
steamUser.on('loggedOn', async () => {
logger.info(`login to Steam as ${steamUser.steamID}`);
steamUser.webLogOn();
});
steamUser.on('webSession', async (sessionID, cookies) => {
steamCommunity.setCookies(cookies);
steamCommunity.startConfirmationChecker(10000, config.identitySecret);
});
async function getUserInfo(steamID, onUserInfoReceived) {
@ -85,6 +93,7 @@ async function getUserInfo(steamID, onUserInfoReceived) {
module.exports = {
steamUser: steamUser,
steamCommunity: steamCommunity,
getUserInfo: getUserInfo,
}

52
package-lock.json generated
View File

@ -10,6 +10,7 @@
"license": "ISC",
"dependencies": {
"@matteo.collina/dateformat": "^5.0.1",
"axios": "^1.7.7",
"steam-totp": "^2.1.2",
"steam-user": "^5.0.8",
"steamcommunity": "^3.48.2",
@ -223,6 +224,31 @@
"resolved": "https://registry.npmjs.org/aws4/-/aws4-1.12.0.tgz",
"integrity": "sha512-NmWvPnx0F1SfrQbYwOi7OeaNGokp9XhzNioJ/CSBs8Qa4vxug81mhJEAVZwxXuBmYB5KDRfMq/F3RR0BIU7sWg=="
},
"node_modules/axios": {
"version": "1.7.7",
"resolved": "https://mvn.tursom.cn:20080/repository/npm/axios/-/axios-1.7.7.tgz",
"integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==",
"license": "MIT",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/axios/node_modules/form-data": {
"version": "4.0.1",
"resolved": "https://mvn.tursom.cn:20080/repository/npm/form-data/-/form-data-4.0.1.tgz",
"integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==",
"license": "MIT",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/bcrypt-pbkdf": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz",
@ -511,6 +537,26 @@
"resolved": "https://registry.npmjs.org/fn.name/-/fn.name-1.1.0.tgz",
"integrity": "sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw=="
},
"node_modules/follow-redirects": {
"version": "1.15.9",
"resolved": "https://mvn.tursom.cn:20080/repository/npm/follow-redirects/-/follow-redirects-1.15.9.tgz",
"integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"license": "MIT",
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/forever-agent": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/forever-agent/-/forever-agent-0.6.1.tgz",
@ -891,6 +937,12 @@
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://mvn.tursom.cn:20080/repository/npm/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
"license": "MIT"
},
"node_modules/psl": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz",

View File

@ -11,6 +11,7 @@
"license": "ISC",
"dependencies": {
"@matteo.collina/dateformat": "^5.0.1",
"axios": "^1.7.7",
"steam-totp": "^2.1.2",
"steam-user": "^5.0.8",
"steamcommunity": "^3.48.2",