Skip to content
This repository was archived by the owner on Aug 18, 2023. It is now read-only.

Commit 0001325

Browse files
authored
Merge pull request #13 from XSPGMike/feature/history
Added support for history
2 parents e1a1bb1 + 9605d1c commit 0001325

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

dist/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,24 @@ class ChatBot {
319319
return e;
320320
}
321321
}
322+
async getHistory(bot) {
323+
try {
324+
let response = await this.makeRequest({
325+
query: `${queries.chatPaginationQuery}`,
326+
variables: {
327+
before: null,
328+
bot: bot,
329+
last: 25,
330+
},
331+
});
332+
for (const { node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) {
333+
console.log(`${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n`);
334+
}
335+
}
336+
catch (e) {
337+
console.log("There has been an error while fetching your history!");
338+
}
339+
}
322340
async getResponse(bot) {
323341
let text;
324342
let state;
@@ -425,6 +443,7 @@ class ChatBot {
425443
let helpMsg = "Available commands: !help !exit, !clear, !submit" +
426444
"\n!help - show this message" +
427445
"\n!exit - exit the chat" +
446+
"\n!history - get the last 25 messages" +
428447
"\n!clear - clear chat history" +
429448
"\n!submit - submit prompt";
430449
// await this.clearContext(this.chatId);
@@ -485,6 +504,11 @@ class ChatBot {
485504
}
486505
submitedPrompt = "";
487506
}
507+
else if (prompt === "!history") {
508+
spinner.start("Loading history...");
509+
await this.getHistory(this.bot);
510+
spinner.stop();
511+
}
488512
else {
489513
submitedPrompt += prompt + "\n";
490514
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"type": "module",
77
"scripts": {
88
"compile": "tsc",
9-
"dev": "npm run compile && node ./dist/index.js",
9+
"dev": "npm run compile && node ./dist/cli.js",
1010
"cli": "node ./dist/cli.js"
1111
},
1212
"keywords": [

src/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,28 @@ class ChatBot {
350350
}
351351
}
352352

353+
public async getHistory(bot: string): Promise<any> {
354+
try {
355+
let response = await this.makeRequest({
356+
query: `${queries.chatPaginationQuery}`,
357+
variables: {
358+
before: null,
359+
bot: bot,
360+
last: 25,
361+
},
362+
});
363+
364+
for(const {node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) {
365+
console.log(
366+
`${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`,
367+
`${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n`
368+
)
369+
}
370+
} catch(e) {
371+
console.log("There has been an error while fetching your history!")
372+
}
373+
}
374+
353375
public async getResponse(bot: string): Promise<any> {
354376
let text: string
355377
let state: string
@@ -462,6 +484,7 @@ class ChatBot {
462484
let helpMsg = "Available commands: !help !exit, !clear, !submit" +
463485
"\n!help - show this message" +
464486
"\n!exit - exit the chat" +
487+
"\n!history - get the last 25 messages" +
465488
"\n!clear - clear chat history" +
466489
"\n!submit - submit prompt";
467490

@@ -519,6 +542,10 @@ class ChatBot {
519542
console.log(response.data);
520543
}
521544
submitedPrompt = "";
545+
} else if(prompt === "!history") {
546+
spinner.start("Loading history...")
547+
await this.getHistory(this.bot)
548+
spinner.stop()
522549
} else {
523550
submitedPrompt += prompt + "\n";
524551
}

0 commit comments

Comments
 (0)