@codebam/cf-workers-telegram-bot 7.20.0 → 7.23.0
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/main.d.ts +1 -9
- package/dist/main.js +1 -2
- package/dist/telegram_api.d.ts +35 -5
- package/dist/telegram_api.js +33 -0
- package/dist/telegram_bot.d.ts +20 -0
- package/dist/telegram_bot.js +20 -0
- package/dist/telegram_execution_context.d.ts +34 -9
- package/dist/telegram_execution_context.js +31 -14
- package/dist/types.d.ts +1 -9
- package/dist/types.js +1 -2
- package/package.json +1 -1
- package/dist/api.d.ts +0 -30
- package/dist/api.js +0 -37
- package/dist/ctx.d.ts +0 -19
- package/dist/ctx.js +0 -109
- package/dist/main/src/TelegramBot.1.d.ts +0 -0
- package/dist/main/src/TelegramBot.1.js +0 -1
- package/dist/main/src/TelegramBot.d.ts +0 -0
- package/dist/main/src/TelegramBot.js +0 -1
- package/dist/main/src/api.d.ts +0 -14
- package/dist/main/src/api.js +0 -21
- package/dist/main/src/bot_api.d.ts +0 -10
- package/dist/main/src/bot_api.js +0 -11
- package/dist/main/src/commands/ban.d.ts +0 -5
- package/dist/main/src/commands/ban.js +0 -8
- package/dist/main/src/commands/bored.d.ts +0 -5
- package/dist/main/src/commands/bored.js +0 -7
- package/dist/main/src/commands/clear.d.ts +0 -5
- package/dist/main/src/commands/clear.js +0 -14
- package/dist/main/src/commands/code.d.ts +0 -5
- package/dist/main/src/commands/code.js +0 -4
- package/dist/main/src/commands/commandlist.d.ts +0 -5
- package/dist/main/src/commands/commandlist.js +0 -1
- package/dist/main/src/commands/dog.d.ts +0 -5
- package/dist/main/src/commands/dog.js +0 -7
- package/dist/main/src/commands/duckduckgo.d.ts +0 -5
- package/dist/main/src/commands/duckduckgo.js +0 -42
- package/dist/main/src/commands/epoch.d.ts +0 -5
- package/dist/main/src/commands/epoch.js +0 -4
- package/dist/main/src/commands/getchatinfo.d.ts +0 -5
- package/dist/main/src/commands/getchatinfo.js +0 -1
- package/dist/main/src/commands/image.d.ts +0 -5
- package/dist/main/src/commands/image.js +0 -19
- package/dist/main/src/commands/joke.d.ts +0 -5
- package/dist/main/src/commands/joke.js +0 -7
- package/dist/main/src/commands/kanye.d.ts +0 -5
- package/dist/main/src/commands/kanye.js +0 -8
- package/dist/main/src/commands/mute.d.ts +0 -5
- package/dist/main/src/commands/mute.js +0 -8
- package/dist/main/src/commands/ping.d.ts +0 -5
- package/dist/main/src/commands/ping.js +0 -1
- package/dist/main/src/commands/question.d.ts +0 -5
- package/dist/main/src/commands/question.js +0 -74
- package/dist/main/src/commands/roll.d.ts +0 -5
- package/dist/main/src/commands/roll.js +0 -6
- package/dist/main/src/commands/sean.d.ts +0 -5
- package/dist/main/src/commands/sean.js +0 -76
- package/dist/main/src/commands/start.d.ts +0 -5
- package/dist/main/src/commands/start.js +0 -1
- package/dist/main/src/commands/toss.d.ts +0 -5
- package/dist/main/src/commands/toss.js +0 -1
- package/dist/main/src/commands/translate.d.ts +0 -5
- package/dist/main/src/commands/translate.js +0 -26
- package/dist/main/src/handler.d.ts +0 -12
- package/dist/main/src/handler.js +0 -40
- package/dist/main/src/libs.d.ts +0 -5
- package/dist/main/src/libs.js +0 -7
- package/dist/main/src/main.d.ts +0 -4
- package/dist/main/src/main.js +0 -4
- package/dist/main/src/telegram_api.d.ts +0 -34
- package/dist/main/src/telegram_api.js +0 -192
- package/dist/main/src/telegram_bot.d.ts +0 -20
- package/dist/main/src/telegram_bot.js +0 -116
- package/dist/main/src/telegram_commands.d.ts +0 -22
- package/dist/main/src/telegram_commands.js +0 -21
- package/dist/main/src/telegram_webhook.d.ts +0 -10
- package/dist/main/src/telegram_webhook.js +0 -20
- package/dist/main/src/types/Balance.d.ts +0 -6
- package/dist/main/src/types/Balance.js +0 -1
- package/dist/main/src/types/Bored.d.ts +0 -10
- package/dist/main/src/types/Bored.js +0 -1
- package/dist/main/src/types/ChatPermissions.d.ts +0 -17
- package/dist/main/src/types/ChatPermissions.js +0 -1
- package/dist/main/src/types/Command.d.ts +0 -5
- package/dist/main/src/types/Command.js +0 -1
- package/dist/main/src/types/CommandArgs.d.ts +0 -5
- package/dist/main/src/types/CommandArgs.js +0 -1
- package/dist/main/src/types/Commands.d.ts +0 -3
- package/dist/main/src/types/Commands.js +0 -1
- package/dist/main/src/types/Config.d.ts +0 -20
- package/dist/main/src/types/Config.js +0 -33
- package/dist/main/src/types/DDGQueryResponse.d.ts +0 -12
- package/dist/main/src/types/DDGQueryResponse.js +0 -1
- package/dist/main/src/types/DataTypes.d.ts +0 -2
- package/dist/main/src/types/DataTypes.js +0 -1
- package/dist/main/src/types/Joke.d.ts +0 -20
- package/dist/main/src/types/Joke.js +0 -1
- package/dist/main/src/types/Kanye.d.ts +0 -4
- package/dist/main/src/types/Kanye.js +0 -1
- package/dist/main/src/types/Kv.d.ts +0 -3
- package/dist/main/src/types/Kv.js +0 -1
- package/dist/main/src/types/PartialTelegramUpdate.d.ts +0 -11
- package/dist/main/src/types/PartialTelegramUpdate.js +0 -1
- package/dist/main/src/types/TelegramChat.d.ts +0 -21
- package/dist/main/src/types/TelegramChat.js +0 -1
- package/dist/main/src/types/TelegramCommand.d.ts +0 -5
- package/dist/main/src/types/TelegramCommand.js +0 -1
- package/dist/main/src/types/TelegramFrom.d.ts +0 -8
- package/dist/main/src/types/TelegramFrom.js +0 -1
- package/dist/main/src/types/TelegramInlineQuery.d.ts +0 -9
- package/dist/main/src/types/TelegramInlineQuery.js +0 -1
- package/dist/main/src/types/TelegramInlineQueryResult.d.ts +0 -6
- package/dist/main/src/types/TelegramInlineQueryResult.js +0 -8
- package/dist/main/src/types/TelegramInlineQueryResultArticle.d.ts +0 -8
- package/dist/main/src/types/TelegramInlineQueryResultArticle.js +0 -15
- package/dist/main/src/types/TelegramInlineQueryResultPhoto.d.ts +0 -13
- package/dist/main/src/types/TelegramInlineQueryResultPhoto.js +0 -19
- package/dist/main/src/types/TelegramInlineQueryType.d.ts +0 -2
- package/dist/main/src/types/TelegramInlineQueryType.js +0 -1
- package/dist/main/src/types/TelegramInputMessageContent.d.ts +0 -5
- package/dist/main/src/types/TelegramInputMessageContent.js +0 -1
- package/dist/main/src/types/TelegramMessage.d.ts +0 -43
- package/dist/main/src/types/TelegramMessage.js +0 -1
- package/dist/main/src/types/TelegramMessageEntity.d.ts +0 -10
- package/dist/main/src/types/TelegramMessageEntity.js +0 -1
- package/dist/main/src/types/TelegramPhotoSize.d.ts +0 -8
- package/dist/main/src/types/TelegramPhotoSize.js +0 -1
- package/dist/main/src/types/TelegramUpdate.d.ts +0 -13
- package/dist/main/src/types/TelegramUpdate.js +0 -36
- package/dist/main/src/types/TelegramUser.d.ts +0 -12
- package/dist/main/src/types/TelegramUser.js +0 -1
- package/dist/main/src/types/Update.d.ts +0 -3
- package/dist/main/src/types/Update.js +0 -2
- package/dist/main/src/types/WebhookCommands.d.ts +0 -4
- package/dist/main/src/types/WebhookCommands.js +0 -2
- package/dist/main/src/types/localhost.d.ts +0 -3
- package/dist/main/src/types/localhost.js +0 -1
- package/dist/main/src/types/types.d.ts +0 -1
- package/dist/main/src/types/types.js +0 -1
- package/dist/main/src/types.d.ts +0 -26
- package/dist/main/src/types.js +0 -8
- package/dist/main/src/webhook.d.ts +0 -10
- package/dist/main/src/webhook.js +0 -15
- package/dist/types/Balance.d.ts +0 -6
- package/dist/types/Balance.js +0 -1
- package/dist/types/Bored.d.ts +0 -10
- package/dist/types/Bored.js +0 -1
- package/dist/types/DDGQueryResponse.d.ts +0 -12
- package/dist/types/DDGQueryResponse.js +0 -1
- package/dist/types/Joke.d.ts +0 -20
- package/dist/types/Joke.js +0 -1
- package/dist/types/Kanye.d.ts +0 -4
- package/dist/types/Kanye.js +0 -1
- package/dist/types/Kv.d.ts +0 -3
- package/dist/types/Kv.js +0 -1
- package/dist/types/SerializableData.d.ts +0 -2
- package/dist/types/SerializableData.js +0 -1
- package/dist/types/localhost.d.ts +0 -3
- package/dist/types/localhost.js +0 -1
- package/dist/worker/src/worker.d.ts +0 -18
- package/dist/worker/src/worker.js +0 -81
package/dist/main/src/api.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import { SerializableData, TelegramInlineQueryResultArticle } from './types';
|
|
3
|
-
export default class API {
|
|
4
|
-
static getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): string;
|
|
5
|
-
static sendMessage(botApi: string, data: {
|
|
6
|
-
reply_to_message_id: number | string;
|
|
7
|
-
chat_id: number | string;
|
|
8
|
-
text: string;
|
|
9
|
-
}): Promise<Response>;
|
|
10
|
-
static answerInline(botApi: string, data: {
|
|
11
|
-
inline_query_id: number | string;
|
|
12
|
-
results: TelegramInlineQueryResultArticle;
|
|
13
|
-
}): Promise<Response>;
|
|
14
|
-
}
|
package/dist/main/src/api.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export default class API {
|
|
2
|
-
static getApiUrl(botApi, slug, data) {
|
|
3
|
-
const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
|
|
4
|
-
const params = new URLSearchParams();
|
|
5
|
-
for (const i in data) {
|
|
6
|
-
params.append(i, data[i].toString());
|
|
7
|
-
}
|
|
8
|
-
return `${request}?${params}`;
|
|
9
|
-
}
|
|
10
|
-
static async sendMessage(botApi, data) {
|
|
11
|
-
const url = this.getApiUrl(botApi, 'sendMessage', data);
|
|
12
|
-
return await fetch(url);
|
|
13
|
-
}
|
|
14
|
-
static async answerInline(botApi, data) {
|
|
15
|
-
const url = this.getApiUrl(botApi, 'answerInlineQuery', {
|
|
16
|
-
inline_query_id: data.inline_query_id,
|
|
17
|
-
results: JSON.stringify(data.results),
|
|
18
|
-
});
|
|
19
|
-
return await fetch(url);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import Handler from './handler';
|
|
3
|
-
import { Commands, Config, Update } from './types';
|
|
4
|
-
export default class BotApi {
|
|
5
|
-
commands: Commands;
|
|
6
|
-
webhook: Config['webhook'];
|
|
7
|
-
handler: Handler;
|
|
8
|
-
update: (update: Update) => Promise<Response>;
|
|
9
|
-
constructor(config: Partial<Config>);
|
|
10
|
-
}
|
package/dist/main/src/bot_api.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => {
|
|
2
|
-
const chat_id = update.message?.reply_to_message?.chat.id;
|
|
3
|
-
const user_id = update.message?.reply_to_message?.from.id;
|
|
4
|
-
if (chat_id && user_id) {
|
|
5
|
-
return self.banChatMember(chat_id, user_id, 0, true);
|
|
6
|
-
}
|
|
7
|
-
return new Response('ok');
|
|
8
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => fetch('https://boredapi.com/api/activity/')
|
|
3
|
-
.then((response) => response.json())
|
|
4
|
-
.then((json) => json)
|
|
5
|
-
.then((bored_response) => update.inline_query
|
|
6
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
7
|
-
: self.sendMessage(update.message?.chat.id ?? 0, bored_response.activity));
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => {
|
|
3
|
-
const { success } = await self.db
|
|
4
|
-
.prepare('DELETE FROM Messages WHERE userId=?')
|
|
5
|
-
.bind(update.inline_query ? update.inline_query.from.id : update.message?.from.id)
|
|
6
|
-
.run();
|
|
7
|
-
if (success) {
|
|
8
|
-
if (update.inline_query) {
|
|
9
|
-
return self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle('_')]);
|
|
10
|
-
}
|
|
11
|
-
return self.sendMessage(update.message?.chat.id ?? 0, '_');
|
|
12
|
-
}
|
|
13
|
-
return self.sendMessage(update.message?.chat.id ?? 0, 'failed');
|
|
14
|
-
};
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => ((url) => update.inline_query
|
|
3
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(url)])
|
|
4
|
-
: self.sendMessage(update.message?.chat.id ?? 0, url))('https://github.com/codebam/cf-workers-telegram-bot');
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, `${Object.keys(self.commands).join('\n')}`, 'HTML');
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultPhoto } from '../types';
|
|
2
|
-
export default async (self, update) => fetch('https://shibe.online/api/shibes')
|
|
3
|
-
.then((response) => response.json())
|
|
4
|
-
.then((json) => json)
|
|
5
|
-
.then((shibe_response) => update.inline_query
|
|
6
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
|
|
7
|
-
: self.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { addSearchParams } from '../libs';
|
|
2
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
3
|
-
export default async (self, update, args) => ((query) => ((duckduckgo_url) => update.inline_query && query === ''
|
|
4
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle('https://duckduckgo.com')])
|
|
5
|
-
: update.inline_query
|
|
6
|
-
? fetch(addSearchParams(new URL('https://api.duckduckgo.com'), {
|
|
7
|
-
q: query,
|
|
8
|
-
format: 'json',
|
|
9
|
-
t: 'telegram_bot',
|
|
10
|
-
no_redirect: '1',
|
|
11
|
-
}).href).then((response) => response
|
|
12
|
-
.json()
|
|
13
|
-
.then((results) => results)
|
|
14
|
-
.then((ddg_response) => ((instant_answer_url, thumb_url, default_thumb_url = 'https://duckduckgo.com/assets/icons/meta/DDG-icon_256x256.png') => self.answerInlineQuery(update.inline_query?.id ?? 0, instant_answer_url !== ''
|
|
15
|
-
? [
|
|
16
|
-
new TelegramInlineQueryResultArticle(`${instant_answer_url}\n\n<a href="${addSearchParams(new URL(duckduckgo_url), {
|
|
17
|
-
q: args
|
|
18
|
-
.slice(2)
|
|
19
|
-
.join(' ')
|
|
20
|
-
.replace(/^!\w* /, ''),
|
|
21
|
-
}).href}">Results From DuckDuckGo</a>`, instant_answer_url, 'HTML', thumb_url),
|
|
22
|
-
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, '', default_thumb_url),
|
|
23
|
-
]
|
|
24
|
-
: [new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, '', default_thumb_url)], 3600))(ddg_response.Redirect ?? ddg_response.AbstractURL, ddg_response.Redirect === ''
|
|
25
|
-
? `https://duckduckgo.com${ddg_response.Image !== '' && ddg_response.Image
|
|
26
|
-
? ddg_response.Image
|
|
27
|
-
: ddg_response.RelatedTopics.length !== 0 && ddg_response.RelatedTopics[0].Icon.URL !== ''
|
|
28
|
-
? ddg_response.RelatedTopics[0].Icon.URL
|
|
29
|
-
: '/i/f96d4798.png'}`
|
|
30
|
-
: '')))
|
|
31
|
-
: self.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))(query === ''
|
|
32
|
-
? 'https://duckduckgo.com'
|
|
33
|
-
: (() => {
|
|
34
|
-
if (query[0][0] !== '/') {
|
|
35
|
-
return addSearchParams(new URL('https://duckduckgo.com'), {
|
|
36
|
-
q: query,
|
|
37
|
-
}).href;
|
|
38
|
-
}
|
|
39
|
-
return addSearchParams(new URL('https://duckduckgo.com'), {
|
|
40
|
-
q: query.split(' ').slice(1).join(' '),
|
|
41
|
-
}).href;
|
|
42
|
-
})()))(args.join(' '));
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => ((seconds) => update.inline_query
|
|
3
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
|
|
4
|
-
: self.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, `<pre>${JSON.stringify(update.message?.chat ?? 0)}</pre>`, 'HTML');
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export default async (self, update, args) => {
|
|
2
|
-
let _prompt;
|
|
3
|
-
if (args[0][0] === '/') {
|
|
4
|
-
_prompt = args.slice(1).join(' ');
|
|
5
|
-
}
|
|
6
|
-
else {
|
|
7
|
-
_prompt = args.join(' ');
|
|
8
|
-
}
|
|
9
|
-
if (_prompt === '') {
|
|
10
|
-
_prompt = '';
|
|
11
|
-
}
|
|
12
|
-
const inputs = { prompt: _prompt, num_steps: 20 };
|
|
13
|
-
await self.sendMessage(update.message?.chat.id ?? 0, 'image is processing. please wait...');
|
|
14
|
-
const response = await self.ai.run('@cf/stabilityai/stable-diffusion-xl-base-1.0', inputs);
|
|
15
|
-
const id = crypto.randomUUID();
|
|
16
|
-
await self.r2.put(id, response);
|
|
17
|
-
const url = 'https://r2.seanbehan.ca/' + id;
|
|
18
|
-
return self.sendPhoto(update.message?.chat.id ?? 0, url);
|
|
19
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => fetch('https://v2.jokeapi.dev/joke/Any?safe-mode')
|
|
3
|
-
.then((response) => response.json())
|
|
4
|
-
.then((joke) => joke)
|
|
5
|
-
.then((joke_response) => ((message) => update.inline_query
|
|
6
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message, joke_response.joke ?? joke_response.setup, 'HTML')], 0)
|
|
7
|
-
: self.sendMessage(update.message?.chat.id ?? 0, message, 'HTML'))(joke_response.joke ?? `${joke_response.setup}\n\n<tg-spoiler>${joke_response.delivery}</tg-spoiler>`));
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => fetch('https://api.kanye.rest')
|
|
3
|
-
.then((response) => response.json())
|
|
4
|
-
.then((json) => json)
|
|
5
|
-
.then((json) => ((message) => update.inline_query
|
|
6
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message)])
|
|
7
|
-
: self.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
8
|
-
.catch(() => new Response('Failed to parse JSON'));
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => {
|
|
2
|
-
const chat_id = update.message?.reply_to_message?.chat.id;
|
|
3
|
-
const user_id = update.message?.reply_to_message?.from.id;
|
|
4
|
-
if (chat_id && user_id) {
|
|
5
|
-
return self.restrictChatMember(chat_id, user_id, { can_send_messages: false }, false, 0);
|
|
6
|
-
}
|
|
7
|
-
return new Response('ok');
|
|
8
|
-
};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default async (self, update, args) => self.sendMessage(update.message?.chat.id ?? 0, args.length === 1 ? 'pong' : args.slice(1).join(' '));
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update, args) => {
|
|
3
|
-
if (self.ai === undefined) {
|
|
4
|
-
return new Response('ok');
|
|
5
|
-
}
|
|
6
|
-
let _prompt;
|
|
7
|
-
if (args[0][0] === '/') {
|
|
8
|
-
_prompt = args.slice(1).join(' ');
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
_prompt = args.join(' ');
|
|
12
|
-
}
|
|
13
|
-
if (_prompt === '') {
|
|
14
|
-
_prompt = '';
|
|
15
|
-
}
|
|
16
|
-
const results = await (async () => {
|
|
17
|
-
if (self.db) {
|
|
18
|
-
const { results } = await self.db
|
|
19
|
-
.prepare('SELECT * FROM Messages WHERE userId=?')
|
|
20
|
-
.bind(update.inline_query ? update.inline_query.from.id : update.message?.from.id)
|
|
21
|
-
.all();
|
|
22
|
-
return results;
|
|
23
|
-
}
|
|
24
|
-
})();
|
|
25
|
-
const old_messages = (() => {
|
|
26
|
-
if (results) {
|
|
27
|
-
return results.map((col) => ({
|
|
28
|
-
role: 'system',
|
|
29
|
-
content: col.content,
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
return [];
|
|
33
|
-
})();
|
|
34
|
-
const system_prompt = '<s>' +
|
|
35
|
-
[
|
|
36
|
-
`Your name is ${self.bot_name}.`,
|
|
37
|
-
`You are talking to ${update.message?.from.first_name}.`,
|
|
38
|
-
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
39
|
-
`the current date is ${new Date().toString()}`,
|
|
40
|
-
].reduce((acc, cur) => {
|
|
41
|
-
return acc + cur + '\n';
|
|
42
|
-
}) +
|
|
43
|
-
old_messages.reduce((acc, cur) => {
|
|
44
|
-
return acc + cur.content + '\n';
|
|
45
|
-
}, '') +
|
|
46
|
-
'</s>';
|
|
47
|
-
const p = system_prompt + '[INST]' + _prompt + '[/INST]';
|
|
48
|
-
const prompt = p.slice(p.length - 4096, p.length);
|
|
49
|
-
const response = await self.ai
|
|
50
|
-
// @ts-expect-error chat model might not match
|
|
51
|
-
.run(self.chat_model, {
|
|
52
|
-
prompt,
|
|
53
|
-
max_tokens: update.inline_query ? 50 : 596,
|
|
54
|
-
})
|
|
55
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
56
|
-
.then(({ response }) => response.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, '').replace(/<<(\/|)SYS>>/, ''));
|
|
57
|
-
if (self.db) {
|
|
58
|
-
const { success } = await self.db
|
|
59
|
-
.prepare('INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)')
|
|
60
|
-
.bind(crypto.randomUUID(), update.inline_query ? update.inline_query.from.id : update.message?.from.id, '[INST] ' + _prompt + ' [/INST]' + '\n' + response)
|
|
61
|
-
.run();
|
|
62
|
-
if (!success) {
|
|
63
|
-
console.log('failed to insert data into d1');
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (response === '') {
|
|
67
|
-
self.clear(self, update);
|
|
68
|
-
return self.question(self, update, args);
|
|
69
|
-
} // sometimes llama2 doesn't respond when given lots of system prompts
|
|
70
|
-
if (update.inline_query) {
|
|
71
|
-
return self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(response)]);
|
|
72
|
-
}
|
|
73
|
-
return self.sendMessage(update.message?.chat.id ?? 0, response, '', false, false, update.message?.message_id);
|
|
74
|
-
};
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update, args) => ((outcome, message) => update.inline_query
|
|
3
|
-
? self.answerInlineQuery(update.inline_query.id, [
|
|
4
|
-
new TelegramInlineQueryResultArticle(message(update.inline_query.from.username, update.inline_query.from.first_name, outcome)),
|
|
5
|
-
])
|
|
6
|
-
: self.sendMessage(update.message?.chat.id ?? 0, message(update.message?.from.username ?? '', update.message?.from.first_name ?? '', outcome)))(Math.floor(Math.random() * (parseInt(args[1]) || 6 - 1 + 1) + 1), (username, first_name, outcome) => `${first_name ?? username} rolled a ${parseInt(args[1]) || 6} sided die. it landed on ${outcome}`);
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update, args) => {
|
|
3
|
-
if (self.ai === undefined) {
|
|
4
|
-
return new Response('ok');
|
|
5
|
-
}
|
|
6
|
-
let _prompt;
|
|
7
|
-
if (args[0][0] === '/') {
|
|
8
|
-
_prompt = args.slice(1).join(' ');
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
_prompt = args.join(' ');
|
|
12
|
-
}
|
|
13
|
-
if (_prompt === '') {
|
|
14
|
-
_prompt = '';
|
|
15
|
-
}
|
|
16
|
-
const results = await (async () => {
|
|
17
|
-
if (self.db) {
|
|
18
|
-
const { results } = await self.db
|
|
19
|
-
.prepare('SELECT * FROM Messages WHERE userId=?')
|
|
20
|
-
.bind(update.inline_query ? update.inline_query.from.id : update.message?.from.id)
|
|
21
|
-
.all();
|
|
22
|
-
return results;
|
|
23
|
-
}
|
|
24
|
-
})();
|
|
25
|
-
const old_messages = (() => {
|
|
26
|
-
if (results) {
|
|
27
|
-
return results.map((col) => ({
|
|
28
|
-
role: 'system',
|
|
29
|
-
content: col.content,
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
return [];
|
|
33
|
-
})();
|
|
34
|
-
const system_prompt = '<s>' +
|
|
35
|
-
[
|
|
36
|
-
`Your name is ${self.bot_name}.`,
|
|
37
|
-
`You are talking to ${update.message?.from.first_name}.`,
|
|
38
|
-
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
39
|
-
`the current date is ${new Date().toString()}`,
|
|
40
|
-
'Sean Behan is a full stack developer who goes by the username codebam.',
|
|
41
|
-
'Sean Behan likes programming and video games.',
|
|
42
|
-
"Pretend to be Sean Behan but don't make things up.",
|
|
43
|
-
].reduce((acc, cur) => {
|
|
44
|
-
return acc + cur + '\n';
|
|
45
|
-
}) +
|
|
46
|
-
old_messages.reduce((acc, cur) => {
|
|
47
|
-
return acc + cur.content + '\n';
|
|
48
|
-
}, '') +
|
|
49
|
-
'</s>';
|
|
50
|
-
const p = system_prompt + '[INST]' + _prompt + '[/INST]';
|
|
51
|
-
const prompt = p.slice(p.length - 4096, p.length);
|
|
52
|
-
const response = await self.ai
|
|
53
|
-
// @ts-expect-error model might not match
|
|
54
|
-
.run(self.chat_model, {
|
|
55
|
-
prompt,
|
|
56
|
-
max_tokens: 596,
|
|
57
|
-
})
|
|
58
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
59
|
-
.then(({ response }) => response
|
|
60
|
-
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, '')
|
|
61
|
-
.replace(/<<(\/|)SYS>>/, '')
|
|
62
|
-
.replace(/[OUT]/, ''));
|
|
63
|
-
if (self.db) {
|
|
64
|
-
const { success } = await self.db
|
|
65
|
-
.prepare('INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)')
|
|
66
|
-
.bind(crypto.randomUUID(), update.inline_query ? update.inline_query.from.id : update.message?.from.id, '[INST] ' + _prompt + ' [/INST]' + '\n' + response)
|
|
67
|
-
.run();
|
|
68
|
-
if (!success) {
|
|
69
|
-
console.log('failed to insert data into d1');
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (update.inline_query) {
|
|
73
|
-
return self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(response)]);
|
|
74
|
-
}
|
|
75
|
-
return self.sendMessage(update.message?.chat.id ?? 0, response, '', false, false, update.message?.message_id);
|
|
76
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, `Hello, send me a message to start chatting with ${self.chat_model}`);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? 'heads' : 'tails');
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramBot from '../telegram_bot';
|
|
3
|
-
import { TelegramUpdate } from '../types';
|
|
4
|
-
declare const _default: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
5
|
-
export default _default;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update, args) => {
|
|
3
|
-
if (self.ai === undefined) {
|
|
4
|
-
return new Response('ok');
|
|
5
|
-
}
|
|
6
|
-
let _prompt;
|
|
7
|
-
if (args[0][0] === '/') {
|
|
8
|
-
_prompt = args.slice(1).join(' ');
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
_prompt = args.join(' ');
|
|
12
|
-
}
|
|
13
|
-
if (_prompt === '') {
|
|
14
|
-
_prompt = '';
|
|
15
|
-
}
|
|
16
|
-
const langs = ['french'];
|
|
17
|
-
const inline_articles = await Promise.all(langs.map(async (lang) => {
|
|
18
|
-
const response = await self.ai.run('@cf/meta/m2m100-1.2b', {
|
|
19
|
-
text: _prompt,
|
|
20
|
-
source_lang: lang,
|
|
21
|
-
target_lang: 'english',
|
|
22
|
-
});
|
|
23
|
-
return new TelegramInlineQueryResultArticle(response.translated_text, response.translated_text);
|
|
24
|
-
}));
|
|
25
|
-
return self.answerInlineQuery(update.inline_query?.id ?? 0, inline_articles);
|
|
26
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import BotApi from './bot_api';
|
|
3
|
-
import { Config } from './types';
|
|
4
|
-
export default class Handler {
|
|
5
|
-
configs: Partial<Config>[];
|
|
6
|
-
constructor(configs: Partial<Config>[]);
|
|
7
|
-
getResponse: (_request?: Request, _bot?: BotApi) => Promise<Response>;
|
|
8
|
-
postResponse: (_request?: Request, _bot?: BotApi) => Promise<Response>;
|
|
9
|
-
responses: Record<string, (_request?: Request, _bot?: BotApi) => Promise<Response>>;
|
|
10
|
-
getAccessKeys: (configs: Partial<Config>[]) => Promise<Record<string, Config> | Record<string, never>>;
|
|
11
|
-
handle: (request: Request) => Promise<Response>;
|
|
12
|
-
}
|
package/dist/main/src/handler.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { sha256, log } from './libs';
|
|
2
|
-
import { Config, localhost } from './types';
|
|
3
|
-
export default class Handler {
|
|
4
|
-
configs;
|
|
5
|
-
constructor(configs) {
|
|
6
|
-
this.configs = configs;
|
|
7
|
-
}
|
|
8
|
-
getResponse = async (_request, _bot) => {
|
|
9
|
-
this.getAccessKeys(this.configs).then((access_keys) => Object.keys(access_keys).forEach((key) => log(`${access_keys[key].bot_name} ${((request_url) => `${request_url.origin}${request_url.pathname}`)(new URL(_request?.url ?? localhost))}${key}`)));
|
|
10
|
-
if (_bot?.webhook.token) {
|
|
11
|
-
return _bot.webhook.process(new URL(_request?.url ?? localhost));
|
|
12
|
-
}
|
|
13
|
-
return this.responses.default();
|
|
14
|
-
};
|
|
15
|
-
postResponse = async (_request, _bot) => _bot?.webhook.token === ''
|
|
16
|
-
? this.responses.default()
|
|
17
|
-
: _request
|
|
18
|
-
? _request.json().then((update) => _bot.update(update))
|
|
19
|
-
: this.responses.default();
|
|
20
|
-
responses = {
|
|
21
|
-
GET: this.getResponse,
|
|
22
|
-
POST: this.postResponse,
|
|
23
|
-
default: () => new Promise(() => new Response()),
|
|
24
|
-
};
|
|
25
|
-
getAccessKeys = async (configs) => Promise.all(configs.map((bot_config) => sha256(bot_config.webhook?.token ?? '').then((hash) => {
|
|
26
|
-
console.log(hash);
|
|
27
|
-
return [hash, bot_config];
|
|
28
|
-
}))).then((result) => Object.fromEntries(result));
|
|
29
|
-
// handles the request
|
|
30
|
-
handle = async (request) => this.getAccessKeys(this.configs).then((access_keys) => Object.keys(this.responses).includes(request.method)
|
|
31
|
-
? this.responses[request.method](request, ((key) => {
|
|
32
|
-
return new access_keys[key].api({
|
|
33
|
-
...new Config(),
|
|
34
|
-
...access_keys[key],
|
|
35
|
-
url: new URL(new URL(request.url).origin), // worker url
|
|
36
|
-
handler: this,
|
|
37
|
-
});
|
|
38
|
-
})(new URL(request.url).pathname.substring(1)))
|
|
39
|
-
: this.responses.default());
|
|
40
|
-
}
|
package/dist/main/src/libs.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
export declare const sha256: (text: string) => Promise<string>;
|
|
3
|
-
export declare const log: (obj: any) => any;
|
|
4
|
-
export declare const addSearchParams: (url: URL, params?: Record<string, string>) => URL;
|
|
5
|
-
export declare const undefinedEmpty: <T>(obj: T) => (T & ({} | null))[];
|
package/dist/main/src/libs.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export const sha256 = async (text) => crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)).then((array_buffer) => Array.from(new Uint8Array(array_buffer))
|
|
2
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
3
|
-
.join(''));
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
-
export const log = (obj) => console.log(obj) === undefined && obj;
|
|
6
|
-
export const addSearchParams = (url, params = {}) => new URL(`${url.origin}${url.pathname}?${new URLSearchParams(Object.entries(Object.fromEntries([...Array.from(url.searchParams.entries()), ...Object.entries(params)]))).toString()}`);
|
|
7
|
-
export const undefinedEmpty = (obj) => (obj === undefined ? [] : [obj]);
|