@codebam/cf-workers-telegram-bot 6.5.0 → 7.3.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.
Files changed (69) hide show
  1. package/README.md +9 -23
  2. package/dist/main/src/main.d.ts +1 -8
  3. package/dist/main/src/main.js +1 -8
  4. package/dist/main/src/telegram_bot.d.ts +18 -32
  5. package/dist/main/src/telegram_bot.js +112 -80
  6. package/dist/main/src/types.d.ts +1 -5
  7. package/dist/main/src/types.js +1 -3
  8. package/dist/worker/src/worker.js +79 -92
  9. package/package.json +2 -2
  10. package/dist/main/src/bot_api.d.ts +0 -10
  11. package/dist/main/src/bot_api.js +0 -11
  12. package/dist/main/src/commands/ban.d.ts +0 -5
  13. package/dist/main/src/commands/ban.js +0 -8
  14. package/dist/main/src/commands/bored.d.ts +0 -5
  15. package/dist/main/src/commands/bored.js +0 -7
  16. package/dist/main/src/commands/clear.d.ts +0 -5
  17. package/dist/main/src/commands/clear.js +0 -14
  18. package/dist/main/src/commands/code.d.ts +0 -5
  19. package/dist/main/src/commands/code.js +0 -4
  20. package/dist/main/src/commands/commandlist.d.ts +0 -5
  21. package/dist/main/src/commands/commandlist.js +0 -1
  22. package/dist/main/src/commands/dog.d.ts +0 -5
  23. package/dist/main/src/commands/dog.js +0 -7
  24. package/dist/main/src/commands/duckduckgo.d.ts +0 -5
  25. package/dist/main/src/commands/duckduckgo.js +0 -42
  26. package/dist/main/src/commands/epoch.d.ts +0 -5
  27. package/dist/main/src/commands/epoch.js +0 -4
  28. package/dist/main/src/commands/getchatinfo.d.ts +0 -5
  29. package/dist/main/src/commands/getchatinfo.js +0 -1
  30. package/dist/main/src/commands/image.d.ts +0 -5
  31. package/dist/main/src/commands/image.js +0 -19
  32. package/dist/main/src/commands/joke.d.ts +0 -5
  33. package/dist/main/src/commands/joke.js +0 -7
  34. package/dist/main/src/commands/kanye.d.ts +0 -5
  35. package/dist/main/src/commands/kanye.js +0 -8
  36. package/dist/main/src/commands/mute.d.ts +0 -5
  37. package/dist/main/src/commands/mute.js +0 -8
  38. package/dist/main/src/commands/ping.d.ts +0 -5
  39. package/dist/main/src/commands/ping.js +0 -1
  40. package/dist/main/src/commands/question.d.ts +0 -5
  41. package/dist/main/src/commands/question.js +0 -74
  42. package/dist/main/src/commands/roll.d.ts +0 -5
  43. package/dist/main/src/commands/roll.js +0 -6
  44. package/dist/main/src/commands/sean.d.ts +0 -5
  45. package/dist/main/src/commands/sean.js +0 -76
  46. package/dist/main/src/commands/start.d.ts +0 -5
  47. package/dist/main/src/commands/start.js +0 -1
  48. package/dist/main/src/commands/toss.d.ts +0 -5
  49. package/dist/main/src/commands/toss.js +0 -1
  50. package/dist/main/src/commands/translate.d.ts +0 -5
  51. package/dist/main/src/commands/translate.js +0 -26
  52. package/dist/main/src/handler.d.ts +0 -12
  53. package/dist/main/src/handler.js +0 -40
  54. package/dist/main/src/libs.d.ts +0 -5
  55. package/dist/main/src/libs.js +0 -7
  56. package/dist/main/src/telegram_api.d.ts +0 -34
  57. package/dist/main/src/telegram_api.js +0 -192
  58. package/dist/main/src/telegram_commands.d.ts +0 -22
  59. package/dist/main/src/telegram_commands.js +0 -21
  60. package/dist/main/src/telegram_webhook.d.ts +0 -10
  61. package/dist/main/src/telegram_webhook.js +0 -20
  62. package/dist/main/src/types/Command.d.ts +0 -5
  63. package/dist/main/src/types/Command.js +0 -1
  64. package/dist/main/src/types/Commands.d.ts +0 -3
  65. package/dist/main/src/types/Commands.js +0 -1
  66. package/dist/main/src/types/Config.d.ts +0 -20
  67. package/dist/main/src/types/Config.js +0 -33
  68. package/dist/main/src/webhook.d.ts +0 -10
  69. package/dist/main/src/webhook.js +0 -15
package/README.md CHANGED
@@ -6,15 +6,16 @@
6
6
 
7
7
  serverless telegram bot on cf workers
8
8
 
9
- The original `worker.js` is the content of Nikhil John's
10
- https://github.com/nikhiljohn10/telegram-bot-worker which is licensed with MIT.
11
- My modifications are licensed under the Apache license.
9
+ To get continuous conversation with AI working make sure you add a database
10
+ to your wrangler.toml and initailize it with the schema.sql
12
11
 
13
- The first bot configuration is fully featured, and responds to all received
14
- messages with llama2 if a command isn't found.
12
+ ```sh
13
+ npm i @codebam/cf-workers-telegram-bot
14
+ ```
15
15
 
16
- To get continuous conversation with llama2 working make sure you add a database
17
- to your wrangler.toml and initailize it with the schema.sql
16
+ See `worker.ts` and follow the instructions below.
17
+
18
+ ---
18
19
 
19
20
  To use the deploy button:
20
21
 
@@ -45,19 +46,4 @@ To fork this repo and use wrangler:
45
46
 
46
47
  ## Getting started with cf-workers-telegram-bot
47
48
 
48
- Once you've deployed the bot you can get your Webhook command URL by doing any
49
- of the following.
50
-
51
- - sha256sum(SECRET_TELEGRAM_API_TOKEN) is the path to your webhook commands and
52
- should be put at the end of your worker URL to access commands such as
53
- setting your webhook
54
- - Use `echo -n yoursecretkey | sha256sum` to get the path
55
- - Open the Cloudflare Worker Logs under **Workers > cf-workers-telegram-bot
56
- > Logs > Begin log stream** and make a GET request (open it in your browser)
57
- to your Worker URL and look at the logs to see your Access URL
58
- - Run `wrangler tail --format pretty` from inside your git repository and make
59
- a GET request to your Worker URL
60
-
61
- Example URL for setting the Webhook and dropping pending updates:
62
-
63
- `https://cf-workers-telegram-bot.codebam.workers.dev/a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447?command=set`
49
+ Once you've deployed the bot you can get your Webhook command URL by calling `await bot.webhook.set()`
@@ -1,11 +1,4 @@
1
- import BotApi from './bot_api';
2
- import Handler from './handler';
3
- import * as Libs from './libs';
4
- import TelegramApi from './telegram_api';
5
1
  import TelegramBot from './telegram_bot';
6
- import TelegramCommands from './telegram_commands';
7
- import TelegramWebhook from './telegram_webhook';
8
2
  import * as Types from './types';
9
- import Webhook from './webhook';
10
3
  import worker from '../../worker/src/worker';
11
- export { BotApi, Handler, Libs, TelegramApi, TelegramBot, TelegramCommands, TelegramWebhook, Types, Webhook, worker };
4
+ export { TelegramBot, Types, worker };
@@ -1,11 +1,4 @@
1
- import BotApi from './bot_api';
2
- import Handler from './handler';
3
- import * as Libs from './libs';
4
- import TelegramApi from './telegram_api';
5
1
  import TelegramBot from './telegram_bot';
6
- import TelegramCommands from './telegram_commands';
7
- import TelegramWebhook from './telegram_webhook';
8
2
  import * as Types from './types';
9
- import Webhook from './webhook';
10
3
  import worker from '../../worker/src/worker';
11
- export { BotApi, Handler, Libs, TelegramApi, TelegramBot, TelegramCommands, TelegramWebhook, Types, Webhook, worker };
4
+ export { TelegramBot, Types, worker };
@@ -1,34 +1,20 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import TelegramApi from './telegram_api';
3
- import { Config, Kv, TelegramUpdate } from './types';
4
- export default class TelegramBot extends TelegramApi {
5
- translate: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
6
- clear: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
7
- image: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
8
- question: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
9
- sean: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
10
- code: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
11
- duckduckgo: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
12
- kanye: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
13
- joke: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
14
- dog: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
15
- bored: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
16
- epoch: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
17
- roll: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
18
- commandList: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
19
- toss: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
20
- ping: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
21
- getChatInfo: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
22
- start: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
23
- ban: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
24
- mute: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
25
- url: URL;
26
- kv: Kv;
27
- get_set: KVNamespace;
28
- ai: Ai;
29
- db: D1Database;
30
- r2: R2Bucket;
31
- bot_name: string;
32
- chat_model: string;
33
- constructor(config: Config);
2
+ import { TelegramUpdate } from './types';
3
+ export default class TelegramBot {
4
+ token: string;
5
+ webhook: Webhook;
6
+ update: TelegramUpdate;
7
+ api: URL;
8
+ update_type: string;
9
+ constructor(token: string);
10
+ on(event: string, callback: () => Promise<Response>): this;
11
+ handle(request: Request): Promise<any>;
12
+ reply(message: string): Promise<void>;
34
13
  }
14
+ declare class Webhook {
15
+ api: URL;
16
+ webhook: URL;
17
+ constructor(token: string, request: Request);
18
+ set(): Promise<Response>;
19
+ }
20
+ export {};
@@ -1,82 +1,114 @@
1
- import TelegramApi from './telegram_api';
2
- import translate from './commands/translate';
3
- import clear from './commands/clear';
4
- import image from './commands/image';
5
- import question from './commands/question';
6
- import sean from './commands/sean';
7
- import code from './commands/code';
8
- import duckduckgo from './commands/duckduckgo';
9
- import kanye from './commands/kanye';
10
- import joke from './commands/joke';
11
- import dog from './commands/dog';
12
- import bored from './commands/bored';
13
- import epoch from './commands/epoch';
14
- import roll from './commands/roll';
15
- import commandlist from './commands/commandlist';
16
- import toss from './commands/toss';
17
- import ping from './commands/ping';
18
- import getchatinfo from './commands/getchatinfo';
19
- import start from './commands/start';
20
- import ban from './commands/ban';
21
- import mute from './commands/mute';
22
- export default class TelegramBot extends TelegramApi {
23
- translate;
24
- clear;
25
- image;
26
- question;
27
- sean;
28
- code;
29
- duckduckgo;
30
- kanye;
31
- joke;
32
- dog;
33
- bored;
34
- epoch;
35
- roll;
36
- commandList;
37
- toss;
38
- ping;
39
- getChatInfo;
40
- start;
41
- ban;
42
- mute;
43
- url;
44
- kv;
45
- get_set;
46
- ai;
47
- db;
48
- r2;
49
- bot_name;
50
- chat_model;
51
- constructor(config) {
52
- super(config.commands, config.webhook, config.handler);
53
- this.translate = translate;
54
- this.clear = clear;
55
- this.image = image;
56
- this.question = question;
57
- this.sean = sean;
58
- this.code = code;
59
- this.duckduckgo = duckduckgo;
60
- this.kanye = kanye;
61
- this.joke = joke;
62
- this.dog = dog;
63
- this.bored = bored;
64
- this.epoch = epoch;
65
- this.roll = roll;
66
- this.commandList = commandlist;
67
- this.toss = toss;
68
- this.ping = ping;
69
- this.getChatInfo = getchatinfo;
70
- this.start = start;
71
- this.ban = ban;
72
- this.mute = mute;
73
- this.url = config.url;
74
- this.kv = config.kv;
75
- this.get_set = config.kv?.get_set;
76
- this.ai = config.ai;
77
- this.db = config.db;
78
- this.r2 = config.r2;
79
- this.bot_name = config.bot_name;
80
- this.chat_model = config.chat_model;
1
+ import { TelegramInlineQueryResultArticle, TelegramUpdate } from './types';
2
+ export default class TelegramBot {
3
+ token;
4
+ webhook;
5
+ update;
6
+ api;
7
+ update_type;
8
+ constructor(token) {
9
+ this.token = token;
10
+ this.webhook = new Webhook('', new Request('http://127.0.0.1'));
11
+ this.update = new TelegramUpdate({});
12
+ this.api = new URL('https://api.telegram.org/bot' + token);
13
+ this.update_type = '';
14
+ }
15
+ on(event, callback) {
16
+ // eslint-disable-next-line
17
+ // @ts-ignore TS7053
18
+ this[event] = callback;
19
+ return this;
20
+ }
21
+ async handle(request) {
22
+ this.webhook = new Webhook(this.token, request);
23
+ if (request.method === 'POST') {
24
+ this.update = await request.json();
25
+ }
26
+ const url = new URL(request.url);
27
+ if (`/${this.token}` === url.pathname) {
28
+ switch (url.searchParams.get('command')) {
29
+ case 'set':
30
+ return this.webhook.set();
31
+ default:
32
+ break;
33
+ }
34
+ console.log(this.update);
35
+ if (this.update.message?.text) {
36
+ this.update_type = 'message';
37
+ }
38
+ else if (this.update.inline_query?.query) {
39
+ this.update_type = 'inline';
40
+ }
41
+ let command = 'default';
42
+ let args = [];
43
+ switch (this.update_type) {
44
+ case 'message': {
45
+ // @ts-expect-error already checked above
46
+ args = this.update.message.text.split(' ');
47
+ break;
48
+ }
49
+ case 'inline': {
50
+ // @ts-expect-error already checked above
51
+ args = this.update.inline_query.query.split(' ');
52
+ break;
53
+ }
54
+ default:
55
+ break;
56
+ }
57
+ if (args.at(0)?.startsWith('/')) {
58
+ // @ts-expect-error already checked above
59
+ command = args.at(0).slice(1);
60
+ }
61
+ // eslint-disable-next-line
62
+ // @ts-ignore
63
+ if (!this[command]) {
64
+ command = 'default';
65
+ }
66
+ // eslint-disable-next-line
67
+ // @ts-ignore
68
+ return this[command]?.();
69
+ }
70
+ return new Response('ok');
71
+ }
72
+ async reply(message) {
73
+ switch (this.update_type) {
74
+ case 'message': {
75
+ const request = new URL(this.api + '/sendMessage');
76
+ const params = new URLSearchParams();
77
+ params.append('chat_id', this.update.message?.chat.id.toString() ?? '');
78
+ params.append('reply_to_message_id', this.update.message?.message_id.toString() ?? '');
79
+ params.append('text', message);
80
+ console.log(`${request}?${params}`);
81
+ await fetch(`${request}?${params}`);
82
+ break;
83
+ }
84
+ case 'inline': {
85
+ const inline_request = new URL(this.api + '/answerInlineQuery');
86
+ const inline_params = new URLSearchParams();
87
+ inline_params.append('inline_query_id', this.update.inline_query?.id.toString() ?? '');
88
+ inline_params.append('results', JSON.stringify([new TelegramInlineQueryResultArticle(message)]));
89
+ console.log(`${inline_request}?${inline_params}`);
90
+ await fetch(`${inline_request}?${inline_params}`);
91
+ break;
92
+ }
93
+ default:
94
+ break;
95
+ }
96
+ }
97
+ }
98
+ class Webhook {
99
+ api;
100
+ webhook;
101
+ constructor(token, request) {
102
+ this.api = new URL('https://api.telegram.org/bot' + token);
103
+ this.webhook = new URL(new URL(request.url).origin + `/${token}`);
104
+ }
105
+ async set() {
106
+ const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
107
+ const params = url.searchParams;
108
+ params.append('url', this.webhook.toString());
109
+ params.append('max_connections', '100');
110
+ params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
111
+ params.append('drop_pending_updates', 'true');
112
+ return await fetch(`${url}?${params}`);
81
113
  }
82
114
  }
@@ -1,9 +1,5 @@
1
- import Webhook from './webhook';
2
- import Command from './types/Command';
3
1
  import TelegramCommand from './types/TelegramCommand';
4
- import Commands from './types/Commands';
5
2
  import Kv from './types/Kv';
6
- import Config from './types/Config';
7
3
  import localhost from './types/localhost';
8
4
  import WebhookCommands from './types/WebhookCommands';
9
5
  import Joke from './types/Joke';
@@ -27,4 +23,4 @@ import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultA
27
23
  import DDGQueryResponse from './types/DDGQueryResponse';
28
24
  import ChatPermissions from './types/ChatPermissions';
29
25
  import Kanye from './types/Kanye';
30
- export { Webhook, Command, TelegramCommand, Commands, Kv, Config, localhost, WebhookCommands, Joke, Bored, Balance, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, DDGQueryResponse, ChatPermissions, Kanye, };
26
+ export { TelegramCommand, Kv, localhost, WebhookCommands, Joke, Bored, Balance, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, DDGQueryResponse, ChatPermissions, Kanye, };
@@ -1,5 +1,3 @@
1
- import Webhook from './webhook';
2
- import Config from './types/Config';
3
1
  import localhost from './types/localhost';
4
2
  import WebhookCommands from './types/WebhookCommands';
5
3
  import Update from './types/Update';
@@ -7,4 +5,4 @@ import TelegramUpdate from './types/TelegramUpdate';
7
5
  import TelegramInlineQueryResult from './types/TelegramInlineQueryResult';
8
6
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto';
9
7
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle';
10
- export { Webhook, Config, localhost, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
8
+ export { localhost, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
@@ -1,94 +1,81 @@
1
- import { TelegramCommands, Handler, TelegramWebhook, TelegramBot } from '../../main/src/main';
1
+ import TelegramBot from '../../main/src/telegram_bot';
2
2
  export default {
3
- fetch: async (request, env) => new Handler([
4
- {
5
- bot_name: '@TuxRobot',
6
- api: TelegramBot,
7
- webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN}`), env.SECRET_TELEGRAM_API_TOKEN, new URL(new URL(request.url).origin)),
8
- commands: {
9
- default: TelegramCommands.question,
10
- inline: TelegramCommands.question,
11
- '/ping': TelegramCommands.ping,
12
- '/toss': TelegramCommands.toss,
13
- '/epoch': TelegramCommands.epoch,
14
- '/kanye': TelegramCommands.kanye,
15
- '/bored': TelegramCommands.bored,
16
- '/joke': TelegramCommands.joke,
17
- '/dog': TelegramCommands.dog,
18
- '/roll': TelegramCommands.roll,
19
- '/duckduckgo': TelegramCommands.duckduckgo,
20
- '/code': TelegramCommands.code,
21
- '/commands': TelegramCommands.commandList,
22
- '/question': TelegramCommands.question,
23
- '/clear': TelegramCommands.clear,
24
- '/help': TelegramCommands.commandList,
25
- '/image': TelegramCommands.image,
26
- '/start': TelegramCommands.start,
27
- '/ban': TelegramCommands.ban,
28
- '/mute': TelegramCommands.mute,
29
- },
30
- kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
31
- ai: env.AI,
32
- db: env.DB,
33
- r2: env.R2,
34
- chat_model: env.CHAT_MODEL,
35
- },
36
- {
37
- bot_name: '@duckduckbot',
38
- api: TelegramBot,
39
- webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN2}`), env.SECRET_TELEGRAM_API_TOKEN2, new URL(new URL(request.url).origin)),
40
- commands: {
41
- default: TelegramCommands.duckduckgo,
42
- inline: TelegramCommands.duckduckgo, // default inline response
43
- '/duckduckgo': TelegramCommands.duckduckgo,
44
- '/question': TelegramCommands.question,
45
- '/code': TelegramCommands.code,
46
- '/commands': TelegramCommands.commandList,
47
- '/help': TelegramCommands.commandList,
48
- '/start': TelegramCommands.commandList,
49
- },
50
- ai: env.AI,
51
- db: env.DB,
52
- },
53
- {
54
- bot_name: '@ddggbot',
55
- api: TelegramBot,
56
- webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN3}`), env.SECRET_TELEGRAM_API_TOKEN3, new URL(new URL(request.url).origin)),
57
- commands: {
58
- default: TelegramCommands.duckduckgo,
59
- inline: TelegramCommands.duckduckgo,
60
- '/duckduckgo': TelegramCommands.duckduckgo,
61
- '/question': TelegramCommands.question,
62
- '/code': TelegramCommands.code,
63
- '/commands': TelegramCommands.commandList,
64
- '/help': TelegramCommands.commandList,
65
- '/start': TelegramCommands.commandList,
66
- },
67
- ai: env.AI,
68
- db: env.DB,
69
- },
70
- {
71
- bot_name: '@SeanB_robot',
72
- api: TelegramBot,
73
- webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN4}`), env.SECRET_TELEGRAM_API_TOKEN4, new URL(new URL(request.url).origin)),
74
- commands: {
75
- default: TelegramCommands.sean,
76
- '/clear': TelegramCommands.clear,
77
- '/start': TelegramCommands.commandList,
78
- },
79
- ai: env.AI,
80
- db: env.DB,
81
- },
82
- {
83
- bot_name: '@TranslatePartyBot',
84
- api: TelegramBot,
85
- webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN5}`), env.SECRET_TELEGRAM_API_TOKEN5, new URL(new URL(request.url).origin)),
86
- commands: {
87
- default: TelegramCommands.translate,
88
- inline: TelegramCommands.translate,
89
- '/start': TelegramCommands.commandList,
90
- },
91
- ai: env.AI,
92
- },
93
- ]).handle(request),
3
+ fetch: async (request, env) => {
4
+ const bot = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN);
5
+ await bot
6
+ .on('default', async function () {
7
+ switch (bot.update_type) {
8
+ case 'message': {
9
+ const messages = [
10
+ { role: 'system', content: 'You are a friendly assistant' },
11
+ {
12
+ role: 'user',
13
+ content: bot.update.message?.text?.toString() ?? '',
14
+ },
15
+ ];
16
+ const response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages });
17
+ if ('response' in response) {
18
+ await bot.reply(response.response ?? '');
19
+ }
20
+ break;
21
+ }
22
+ case 'inline': {
23
+ const inline_messages = [
24
+ { role: 'system', content: 'You are a friendly assistant' },
25
+ {
26
+ role: 'user',
27
+ content: bot.update.inline_query?.query.toString() ?? '',
28
+ },
29
+ ];
30
+ const inline_response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages: inline_messages, max_tokens: 50 });
31
+ if ('response' in inline_response) {
32
+ await bot.reply(inline_response.response ?? '');
33
+ }
34
+ break;
35
+ }
36
+ default:
37
+ break;
38
+ }
39
+ return new Response('ok');
40
+ })
41
+ .handle(request.clone());
42
+ const bot2 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN2);
43
+ await bot2
44
+ .on('default', async function () {
45
+ switch (bot2.update_type) {
46
+ case 'message': {
47
+ await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.message?.text?.toString() ?? ''));
48
+ break;
49
+ }
50
+ case 'inline': {
51
+ await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.inline_query?.query ?? ''));
52
+ break;
53
+ }
54
+ default:
55
+ break;
56
+ }
57
+ return new Response('ok');
58
+ })
59
+ .handle(request.clone());
60
+ const bot3 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN3);
61
+ await bot3
62
+ .on('default', async function () {
63
+ switch (bot3.update_type) {
64
+ case 'inline': {
65
+ const { translated_text } = await env.AI.run('@cf/meta/m2m100-1.2b', {
66
+ text: bot3.update.inline_query?.query.toString() ?? '',
67
+ source_lang: 'french',
68
+ target_lang: 'english',
69
+ });
70
+ await bot3.reply(translated_text ?? '');
71
+ break;
72
+ }
73
+ default:
74
+ break;
75
+ }
76
+ return new Response('ok');
77
+ })
78
+ .handle(request.clone());
79
+ return new Response('ok');
80
+ },
94
81
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "6.5.0",
3
+ "version": "7.3.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main/src/main.js",
6
6
  "module": "./dist/main/src/main.js",
@@ -41,5 +41,5 @@
41
41
  "typescript": "^5.4.5",
42
42
  "typescript-eslint": "^7.8.0"
43
43
  },
44
- "gitHead": "843a4525d055dce257c124a0f0a15b8f552b1a47"
44
+ "gitHead": "55015e53958e69fc128e0b1e8116abb249291b0f"
45
45
  }
@@ -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
- }
@@ -1,11 +0,0 @@
1
- export default class BotApi {
2
- commands;
3
- webhook;
4
- handler;
5
- update;
6
- constructor(config) {
7
- this.commands = config.commands;
8
- this.webhook = config.webhook;
9
- this.handler = config.handler;
10
- }
11
- }
@@ -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) => Promise<Response>;
5
- export default _default;
@@ -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,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) => Promise<Response>;
5
- export default _default;
@@ -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,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) => Promise<Response>;
5
- export default _default;
@@ -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,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) => Promise<Response>;
5
- export default _default;