@codebam/cf-workers-telegram-bot 12.5.0 → 12.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/history_manager.d.ts +37 -0
- package/dist/history_manager.js +69 -0
- package/dist/main.d.ts +2 -1
- package/dist/main.js +2 -1
- package/dist/telegram_bot.js +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Class representing a manager for conversation history stored in KV */
|
|
2
|
+
export declare class HistoryManager {
|
|
3
|
+
private kv;
|
|
4
|
+
constructor(kv: KVNamespace);
|
|
5
|
+
private getKey;
|
|
6
|
+
/**
|
|
7
|
+
* Get the conversation history for a user
|
|
8
|
+
* @param userId - the telegram user ID
|
|
9
|
+
* @param threadId - optional thread ID
|
|
10
|
+
* @returns array of messages
|
|
11
|
+
*/
|
|
12
|
+
getHistory(userId: number, threadId?: number): Promise<{
|
|
13
|
+
role: string;
|
|
14
|
+
content: string;
|
|
15
|
+
}[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Add a message and its response to the history
|
|
18
|
+
* @param userId - the telegram user ID
|
|
19
|
+
* @param prompt - the user message
|
|
20
|
+
* @param response - the bot response
|
|
21
|
+
* @param threadId - optional thread ID
|
|
22
|
+
*/
|
|
23
|
+
addMessage(userId: number, prompt: string, response: string, threadId?: number): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Clear the conversation history for a user
|
|
26
|
+
* @param userId - the telegram user ID
|
|
27
|
+
* @param threadId - optional thread ID
|
|
28
|
+
*/
|
|
29
|
+
clearHistory(userId: number, threadId?: number): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get the balance for a user, initializing it if it doesn't exist
|
|
33
|
+
* @param userId - the telegram user ID
|
|
34
|
+
* @param kv - the KV namespace
|
|
35
|
+
* @returns the user's balance
|
|
36
|
+
*/
|
|
37
|
+
export declare function getBalance(userId: number, kv: KVNamespace): Promise<number>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** Class representing a manager for conversation history stored in KV */
|
|
2
|
+
export class HistoryManager {
|
|
3
|
+
kv;
|
|
4
|
+
constructor(kv) {
|
|
5
|
+
this.kv = kv;
|
|
6
|
+
}
|
|
7
|
+
getKey(userId, threadId) {
|
|
8
|
+
return threadId ? `history:${String(userId)}:${String(threadId)}` : `history:${String(userId)}`;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Get the conversation history for a user
|
|
12
|
+
* @param userId - the telegram user ID
|
|
13
|
+
* @param threadId - optional thread ID
|
|
14
|
+
* @returns array of messages
|
|
15
|
+
*/
|
|
16
|
+
async getHistory(userId, threadId) {
|
|
17
|
+
if (!this.kv) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
const history = await this.kv.get(this.getKey(userId, threadId), 'json');
|
|
21
|
+
return history ?? [];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Add a message and its response to the history
|
|
25
|
+
* @param userId - the telegram user ID
|
|
26
|
+
* @param prompt - the user message
|
|
27
|
+
* @param response - the bot response
|
|
28
|
+
* @param threadId - optional thread ID
|
|
29
|
+
*/
|
|
30
|
+
async addMessage(userId, prompt, response, threadId) {
|
|
31
|
+
if (!this.kv) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const history = await this.getHistory(userId, threadId);
|
|
35
|
+
history.push({ role: 'user', content: prompt });
|
|
36
|
+
history.push({ role: 'assistant', content: response });
|
|
37
|
+
const trimmedHistory = history.slice(-20);
|
|
38
|
+
await this.kv.put(this.getKey(userId, threadId), JSON.stringify(trimmedHistory), {
|
|
39
|
+
expirationTtl: 86400
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Clear the conversation history for a user
|
|
44
|
+
* @param userId - the telegram user ID
|
|
45
|
+
* @param threadId - optional thread ID
|
|
46
|
+
*/
|
|
47
|
+
async clearHistory(userId, threadId) {
|
|
48
|
+
if (!this.kv) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
await this.kv.delete(this.getKey(userId, threadId));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the balance for a user, initializing it if it doesn't exist
|
|
56
|
+
* @param userId - the telegram user ID
|
|
57
|
+
* @param kv - the KV namespace
|
|
58
|
+
* @returns the user's balance
|
|
59
|
+
*/
|
|
60
|
+
export async function getBalance(userId, kv) {
|
|
61
|
+
const balanceKey = `balance:${String(userId)}`;
|
|
62
|
+
const balance = await kv.get(balanceKey, 'json');
|
|
63
|
+
if (balance === null) {
|
|
64
|
+
const defaultBalance = 200;
|
|
65
|
+
await kv.put(balanceKey, JSON.stringify(defaultBalance));
|
|
66
|
+
return defaultBalance;
|
|
67
|
+
}
|
|
68
|
+
return balance;
|
|
69
|
+
}
|
package/dist/main.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ import TelegramGuestMessage from './types/TelegramGuestMessage.js';
|
|
|
8
8
|
import PartialTelegramUpdate from './types/PartialTelegramUpdate.js';
|
|
9
9
|
import TelegramInlineQueryType from './types/TelegramInlineQueryType.js';
|
|
10
10
|
import { markdownToHtml, fetchTool } from './utils.js';
|
|
11
|
+
import { HistoryManager, getBalance } from './history_manager.js';
|
|
11
12
|
export default TelegramBot;
|
|
12
|
-
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramVoice, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, markdownToHtml, fetchTool, };
|
|
13
|
+
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramVoice, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, markdownToHtml, fetchTool, HistoryManager, getBalance, };
|
package/dist/main.js
CHANGED
|
@@ -3,5 +3,6 @@ import TelegramExecutionContext from './telegram_execution_context.js';
|
|
|
3
3
|
import Webhook from './webhook.js';
|
|
4
4
|
import TelegramApi from './telegram_api.js';
|
|
5
5
|
import { markdownToHtml, fetchTool } from './utils.js';
|
|
6
|
+
import { HistoryManager, getBalance } from './history_manager.js';
|
|
6
7
|
export default TelegramBot;
|
|
7
|
-
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, };
|
|
8
|
+
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, HistoryManager, getBalance, };
|
package/dist/telegram_bot.js
CHANGED
|
@@ -129,6 +129,9 @@ export default class TelegramBot {
|
|
|
129
129
|
// Then check if there's a command starting with / anywhere in the message
|
|
130
130
|
const commandIndex = args.findIndex((arg) => arg.startsWith('/'));
|
|
131
131
|
if (commandIndex !== -1) {
|
|
132
|
+
if (ctx.update_type === 'message' && ctx.update.message?.chat.type !== 'private') {
|
|
133
|
+
return this.defaultCommand;
|
|
134
|
+
}
|
|
132
135
|
const fullCommand = args[commandIndex];
|
|
133
136
|
const command = fullCommand.substring(1, fullCommand.lastIndexOf('@') > -1 ? fullCommand.lastIndexOf('@') : fullCommand.length);
|
|
134
137
|
if (command in this.commands) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.6.1",
|
|
4
4
|
"description": "serverless telegram bot on cf workers",
|
|
5
5
|
"main": "./dist/main.js",
|
|
6
6
|
"module": "./dist/main.js",
|
|
@@ -61,4 +61,4 @@
|
|
|
61
61
|
"./src/main.ts"
|
|
62
62
|
]
|
|
63
63
|
}
|
|
64
|
-
}
|
|
64
|
+
}
|