samliew / chatexchange Goto Github PK
View Code? Open in Web Editor NEWA node.js API for talking with the Stack Exchange chat
Home Page: https://samliew.github.io/chatexchange/
License: GNU General Public License v3.0
A node.js API for talking with the Stack Exchange chat
Home Page: https://samliew.github.io/chatexchange/
License: GNU General Public License v3.0
I am getting a 404 error when trying to get the eventType
or targetUserId
of a message.
node:internal/process/promises:279
triggerUncaughtException(err, true /* fromPromise */);
^
HTTPError: Response code 404 (Not Found)
at Request.<anonymous> (/home/runner/nlnCommentBot/node_modules/got/dist/source/as-promise/index.js:118:42) {
code: '***_***_***_***_********',
timings: {
start: *************,
socket: *************,
lookup: *************,
connect: *************,
secureConnect: *************,
upload: *************,
response: *************,
end: *************,
error: undefined,
abort: undefined,
phases: {
wait: *,
dns: **,
tcp: **,
tls: **,
request: *,
firstByte: **,
download: *,
total: ***
}
}
}
I replaced a bunch of stuff with *
since I do not know if they are sensitive.
const fetch = require("node-fetch")
const Client = require("chatexchange");
const { ChatEventType } = require("chatexchange");
const botEmail = process.env.botEmail
const botPassWord = process.env.botPassWord
const API_KEY = process.env.Key; // Your API_KEY
const Access_Token = process.env.Token; // Your Access_Token
const Site_Name = 'stackoverflow'; // Change site name if needed
const Room_ID = '167908'; // Room to send message
const main = async () => {
const client = new Client['default']("stackoverflow.com");
async function getComments() {
const response = await fetch(`https://api.stackexchange.com/2.2/comments?page=1&pagesize=100&order=desc&sort=creation&filter=!4(lY7*YmhBbS4j1g_&site=stackoverflow&key=${API_KEY}`)
const data = await response.json()
return data
}
async function filterComments() {
const result = await getComments()
const comments = result.items
const quota = result.quota_remaining
const thanksRegex = new RegExp(`((?:^)@(\\w+)\\b\\s)?thank([\\w.,!\']*)?(\\s[\\w{1,8},.@!:\\-)]*)?(\\s[\\w{1,8},.@!:\\-)]*)?(\\s[\\w{1,8},.@!:\\-)]*)?`)
const gladRegex = new RegExp(`glad(?:\\sto\\shelp|hear)?`)
const goodSolutionRegex = new RegExp("(appreciate|perfect|awesome|amazing|excellent)(?:,|\w{1,2})?(\\s(?:solution|example)?)?")
const solveRegex = new RegExp("solv(\\w{1,3})(\\s(\w{2,5}))\\s(\\w{1,9})?")
const upvoteRegex = new RegExp("(\\w{1,8}\\s)?up(?:\\s)?vote\\s(\\w{0,8})?\\s(\\w{0,8})?")
const workRegex = new RegExp("(\\w{1,5}([\\w{,2}\']*)\\s)?work([\\w*{0,3}!.]*)?(\\s[:\\-)+=.}]*)?")
const saveRegex = new RegExp("save(\\w{1,3})?\\s(\\w{0,4})\\s([\\w{0,6}.!:\\-)]*)?")
const thanksButNotRegex = new RegExp("([\\w{1,8},.@!:)]*\\s)?(love|cheers|great)(\\s[\\w{1,8},.@!:)]*)?")
const smilyFaceRegex = new RegExp(":\\)| :-\\)|;\\)")
const butRegex = new RegExp("not|unfortunate|but|require|need|persists")
const dontRegex = new RegExp("\\b(doesn|don|didn|couldn|can|isn)(['β])?(\\w{1,2})?\\b")
const questionMarkRegex = new RegExp("[?]")
for (let i = 0; i < comments.length; i++) {
const thisComment = comments[i].body_markdown.toLowerCase();
let score = 0
if (thisComment.match(thanksRegex)) {
score += 1
}
if (thisComment.match(gladRegex)) {
score += 1
}
if (thisComment.match(goodSolutionRegex)) {
score += 1
}
if (thisComment.match(solveRegex)) {
score += 1
}
if (thisComment.match(upvoteRegex)) {
score += 1
}
if (thisComment.match(workRegex)) {
score += 1
}
if (thisComment.match(saveRegex)) {
score += 1
}
if (thisComment.match(smilyFaceRegex)) {
score == 1
}
if (thisComment.match(thanksButNotRegex)) {
score += 1
}
if (thisComment.match(butRegex)) {
score -= 1
}
if (thisComment.match(dontRegex)) {
score -= 1
}
if (thisComment.match(questionMarkRegex)) {
score -= 1
}
if (score > 0) {
// sendsa a message
client.send(`${thisComment} \n Score: ${score}`)
}
}
}
await client.login(botEmail, botPassWord);
const me = await client.getMe();
const myProfile = await client.getProfile(me);
const { roomCount } = myProfile;
console.log(`Rooms I am in: ${roomCount}`);
const room = client.getRoom(Room_ID);
const joined = await client.joinRoom(room);
// setTimeout(filterComments(), 60000) // 60000 is 1 minute
if(joined) {
room.on("message", async (msg) => {
const { eventType, targetUserId } = msg;
if (eventType === ChatEventType.USER_MENTIONED && targetUserId === me.id) {
await msg.reply(`Hi @${message.user}. Unfortently I dont have any commands (including feedback) as of now.`);
console.log("Got Message", msg);
}
});
// Connect to the room, and listen for new events
await room.watch();
return;
}
await client.logout();
};
main();
devDependency
@babel/cli was updated from 7.7.7
to 7.8.0
.devDependency
@babel/core was updated from 7.7.7
to 7.8.0
.devDependency
@babel/node was updated from 7.7.7
to 7.8.0
.devDependency
@babel/preset-env was updated from 7.8.0
to 7.8.2
.devDependency
@babel/preset-flow was updated from 7.7.4
to 7.8.0
.π¨ View failing branch.
This version is covered by your current version range and after updating it in your project the build failed.
This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.
babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those donβt help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot π΄
The recent rollout of the new navbar removed the my-profile
CSS class from the logged-in main page on a given host subsequently breaking the ability of the package to use the loginCookie
method that relied on it for confirmation of a successful login. Working on a solution.
The example in the README.md file does not work. On line 9 I get the following error:
/home/runner/nlnCommentBot/index.js:9
const myClient = new Client("stackoverflow.com");
^
ReferenceError: Client is not defined
at main (/home/runner/nlnCommentBot/index.js:9:20)
at Object.<anonymous> (/home/runner/nlnCommentBot/index.js:54:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
I made sure I copied everything correctly and installed the library. The only thing I changed was the login to my own account. My GH repo.
See https://samliew.github.io/chatexchange/classes/Message.default.html:
This function should return Promise<User>
but instead shows Promise<default>
(This happens frequently throughout the docs).
Would be nice to show the actual class names instead of default. (Probably some ES6 thing with the default export).
Ability to make a bot leave a room, or all the rooms:
POST https://chat.stackexchange.com/chats/leave/<room_id>
{
quiet: true
fkey: fkey
}
POST https://chat.stackexchange.com/chats/leave/all
{
quiet: true
fkey: fkey
}
I think this has to do with the phantom user (community perhaps?)
Error message:
/app/node_modules/chatexchange/dist/Browser.js:320
const userId = parseInt($userDiv.attr("href").split("/")[2], 10);
^
TypeError: Cannot read properties of undefined (reading 'split')
at Browser.getTranscript (/app/node_modules/chatexchange/dist/Browser.js:320:54)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async WebsocketEvent._Message_scrapeTranscript (/app/node_modules/chatexchange/dist/Message.js:127:59)
at async lazy (/app/node_modules/chatexchange/dist/utils.js:34:5)
@samliew - I was poking around the repo and noticed that the repo "about" points to the old Pages: danbopes.github.io/chatexchange/
whereas it should point to the https://samliew.github.io/chatexchange/
.
I don't have enough privileges on the repo to update it - could you do that, please?
Other than that, after a couple of battles with outdated dependencies, the repo should be in a workable state - I will continue upgrading it and hopefully will bring it to the 1.0.0 stable release.
@Oaphi Thanks. Now I get a different error with something else.
const botEmail = process.env.botEmail
const botPassWord = process.env.botPassWord
const Client = require("chatexchange");
const { ChatEventType } = require("chatexchange");
const main = async () => {
const client = new Client['default']("stackoverflow.com");
await client.login(botEmail, botPassWord);
const me = await client.getMe();
const myProfile = await client.getProfile(me);
const { roomCount } = myProfile;
console.log(`Rooms I am in: ${roomCount}`);
const room = client.getRoom(167908);
room.ignore(ChatEventType.FILE_ADDED);
const joined = await client.joinRoom(room);
if(joined) {
room.on("message", async (msg) => {
console.log("Got Message", msg);
const { eventType, targetUserId } = msg;
if (eventType === ChatEventType.USER_MENTIONED && targetUserId === me.id) {
await msg.reply("Hello World!");
}
if(eventType === ChatEventType.USER_LEFT) {
await msg.send("See you around!", room);
}
});
// Leave the room after five minutes
setTimeout(async () => {
await room.sendMessage("Bye everyone!");
await client.leaveRoom(room);
}, 3e5);
// Connect to the room, and listen for new events
await room.watch();
return;
}
await client.logout();
};
main();
Rooms I am in: 4
/home/runner/nlnCommentBot/node_modules/chatexchange/dist/Browser.js:308
throw new ChatExchangeError_1.default(`missing time key for room ${roomid}\n\nTime keys\n${report}`);
^
ChatExchangeError: missing time key for room 167908
Time keys
167908 : missing
at Browser.watchRoom (/home/runner/nlnCommentBot/node_modules/chatexchange/dist/Browser.js:308:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Client.watch (/home/runner/nlnCommentBot/node_modules/chatexchange/dist/Client.js:334:20)
at async Room.watch (/home/runner/nlnCommentBot/node_modules/chatexchange/dist/Room.js:178:52)
Originally posted by @not-a-ethan in #204 (comment)
@Oaphi Played around with it and Im getting another error.
/home/runner/nlnCommentBot/index.js:36
msg.send("See you around!", room);
^
TypeError: msg.send is not a function
at Room.<anonymous> (/home/runner/nlnCommentBot/index.js:36:17)
at Room.emit (node:events:527:28)
at Room.emit (node:domain:475:12)
at WebSocket.<anonymous> (/home/runner/nlnCommentBot/node_modules/chatexchange/dist/Client.js:350:25)
at WebSocket.emit (node:events:527:28)
at WebSocket.emit (node:domain:475:12)
at Receiver.receiverOnMessage (/home/runner/nlnCommentBot/node_modules/ws/lib/websocket.js:1178:20)
at Receiver.emit (node:events:527:28)
at Receiver.emit (node:domain:475:12)
Originally posted by @not-a-ethan in #204 (comment)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.