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

Commit

Permalink
Merge pull request #13 from XSPGMike/feature/history
Browse files Browse the repository at this point in the history
Added support for history
  • Loading branch information
muharamdani authored Apr 1, 2023
2 parents e1a1bb1 + 9605d1c commit 0001325
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ class ChatBot {
return e;
}
}
async getHistory(bot) {
try {
let response = await this.makeRequest({
query: `${queries.chatPaginationQuery}`,
variables: {
before: null,
bot: bot,
last: 25,
},
});
for (const { node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) {
console.log(`${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`, `${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n`);
}
}
catch (e) {
console.log("There has been an error while fetching your history!");
}
}
async getResponse(bot) {
let text;
let state;
Expand Down Expand Up @@ -425,6 +443,7 @@ class ChatBot {
let helpMsg = "Available commands: !help !exit, !clear, !submit" +
"\n!help - show this message" +
"\n!exit - exit the chat" +
"\n!history - get the last 25 messages" +
"\n!clear - clear chat history" +
"\n!submit - submit prompt";
// await this.clearContext(this.chatId);
Expand Down Expand Up @@ -485,6 +504,11 @@ class ChatBot {
}
submitedPrompt = "";
}
else if (prompt === "!history") {
spinner.start("Loading history...");
await this.getHistory(this.bot);
spinner.stop();
}
else {
submitedPrompt += prompt + "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"compile": "tsc",
"dev": "npm run compile && node ./dist/index.js",
"dev": "npm run compile && node ./dist/cli.js",
"cli": "node ./dist/cli.js"
},
"keywords": [
Expand Down
27 changes: 27 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,28 @@ class ChatBot {
}
}

public async getHistory(bot: string): Promise<any> {
try {
let response = await this.makeRequest({
query: `${queries.chatPaginationQuery}`,
variables: {
before: null,
bot: bot,
last: 25,
},
});

for(const {node: { messageId, text, authorNickname } } of response.data.chatOfBot.messagesConnection.edges) {
console.log(
`${authorNickname === 'human' ? '\x1b[37m%s\x1b[0m' : '\x1b[32m%s\x1b[0m'}`,
`${authorNickname === 'human' ? 'You' : 'Bot'}: ${text}\n`
)
}
} catch(e) {
console.log("There has been an error while fetching your history!")
}
}

public async getResponse(bot: string): Promise<any> {
let text: string
let state: string
Expand Down Expand Up @@ -462,6 +484,7 @@ class ChatBot {
let helpMsg = "Available commands: !help !exit, !clear, !submit" +
"\n!help - show this message" +
"\n!exit - exit the chat" +
"\n!history - get the last 25 messages" +
"\n!clear - clear chat history" +
"\n!submit - submit prompt";

Expand Down Expand Up @@ -519,6 +542,10 @@ class ChatBot {
console.log(response.data);
}
submitedPrompt = "";
} else if(prompt === "!history") {
spinner.start("Loading history...")
await this.getHistory(this.bot)
spinner.stop()
} else {
submitedPrompt += prompt + "\n";
}
Expand Down

0 comments on commit 0001325

Please sign in to comment.