@codebam/cf-workers-telegram-bot 6.6.0 → 7.4.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 +37 -43
  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 +114 -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 +78 -92
  9. package/package.json +3 -3
  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
@@ -1,22 +1,47 @@
1
- # cf-workers-telegram-bot
2
-
3
- [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/codebam/cf-workers-telegram-bot)
1
+ <h3 align="center">
2
+ <img src="https://raw.githubusercontent.com/codebam/cf-workers-telegram-bot/master/assets/logo.png" width="100" />
3
+ <br/>
4
+ CF workers telegram bot
5
+ <br/>
6
+ </h3>
7
+
8
+ <h6 align="center">
9
+ <a href="https://github.com/codebam/cf-workers-telegram-bot/wiki">Wiki</a>
10
+ ·
11
+ <a href="https://codebam.github.io/cf-workers-telegram-bot-docs/">Docs</a>
12
+ </h6>
13
+
14
+ <p align="center">
15
+ <a href="https://github.com/codebam/cf-workers-telegram-bot/stargazers"> <img src="https://img.shields.io/github/stars/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=starship&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub stars"/></a>
16
+ <a href="https://github.com/codebam/cf-workers-telegram-bot/issues">
17
+ <img src="https://img.shields.io/github/issues/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=gitbook&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub issues"/></a>
18
+ <a href="https://github.com/codebam/cf-workers-telegram-bot"> <img src="https://img.shields.io/github/forks/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=git&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub forks"/></a>
19
+ <a href="https://www.npmjs.com/package/@codebam/cf-workers-telegram-bot"> <img src="https://img.shields.io/npm/v/@codebam/cf-workers-telegram-bot?style=for-the-badge&logo=npm&color=111111&logoColor=ffffff&labelColor=000000" alt="npm version" /></a>
20
+ </p>
4
21
 
5
22
  ![screenshot of cf-workers-telegram-bot](/screenshot.png)
6
23
 
7
- serverless telegram bot on cf workers
24
+ ```sh
25
+ npm i @codebam/cf-workers-telegram-bot
26
+ ```
27
+
28
+ See `worker.ts` for an example of what a bot might look like. Just import from `@codebam/cf-workers-telegram-bot`.
8
29
 
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.
30
+ - `npm create cloudflare@latest`
31
+ - `npx wrangler login`
32
+ - `npx wrangler secret put SECRET_TELEGRAM_API_TOKEN`, set it to your telegram bot token that you got from `@BotFather`
33
+ - `npx wrangler deploy`
34
+ - Open this url in your browser to set your webhook `https://your-worker.username.workers.dev/SECRET_TELEGRAM_API_TOKEN?command=set`
12
35
 
13
- The first bot configuration is fully featured, and responds to all received
14
- messages with llama2 if a command isn't found.
36
+ To set up GitHub actions to deploy when you push, see https://github.com/cloudflare/wrangler-action
15
37
 
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
38
+ ---
18
39
 
19
- To use the deploy button:
40
+ These instructions are for if you want to deploy a copy of the bot along with
41
+ the library. Such as if you need extra API requests that haven't been
42
+ implemented yet.
43
+
44
+ [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/codebam/cf-workers-telegram-bot)
20
45
 
21
46
  - Click the deploy button
22
47
  - Navigate to your new **GitHub repository &gt; Settings &gt; Secrets** and add the following secrets:
@@ -30,34 +55,3 @@ To use the deploy button:
30
55
  ```
31
56
 
32
57
  - Push to `master` to trigger a deploy
33
-
34
- To fork this repo and use wrangler:
35
-
36
- - Click fork
37
- - `npm i -g wrangler`
38
- - `wrangler secret put SECRET_TELEGRAM_API_TOKEN` and set it to your telegram
39
- bot token
40
- - `wrangler d1 create llama2`
41
- - put the database block from the command in your wrangler.toml
42
- - `wrangler d1 execute --remote llama2 --file ./schema.sql`
43
- - `wrangler deploy`
44
- - Done!
45
-
46
- ## Getting started with cf-workers-telegram-bot
47
-
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 &gt; cf-workers-telegram-bot
56
- &gt; Logs &gt; 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`
@@ -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,116 @@
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
+ if (event !== 'on') {
17
+ // eslint-disable-next-line
18
+ // @ts-ignore TS7053
19
+ this[event] = callback;
20
+ }
21
+ return this;
22
+ }
23
+ async handle(request) {
24
+ this.webhook = new Webhook(this.token, request);
25
+ if (request.method === 'POST') {
26
+ this.update = await request.json();
27
+ }
28
+ const url = new URL(request.url);
29
+ if (`/${this.token}` === url.pathname) {
30
+ switch (url.searchParams.get('command')) {
31
+ case 'set':
32
+ return this.webhook.set();
33
+ default:
34
+ break;
35
+ }
36
+ console.log(this.update);
37
+ if (this.update.message?.text) {
38
+ this.update_type = 'message';
39
+ }
40
+ else if (this.update.inline_query?.query) {
41
+ this.update_type = 'inline';
42
+ }
43
+ let command = 'default';
44
+ let args = [];
45
+ switch (this.update_type) {
46
+ case 'message': {
47
+ // @ts-expect-error already checked above
48
+ args = this.update.message.text.split(' ');
49
+ break;
50
+ }
51
+ case 'inline': {
52
+ // @ts-expect-error already checked above
53
+ args = this.update.inline_query.query.split(' ');
54
+ break;
55
+ }
56
+ default:
57
+ break;
58
+ }
59
+ if (args.at(0)?.startsWith('/')) {
60
+ // @ts-expect-error already checked above
61
+ command = args.at(0).slice(1);
62
+ }
63
+ // eslint-disable-next-line
64
+ // @ts-ignore
65
+ if (!this[command] || command === 'on') {
66
+ command = 'default';
67
+ }
68
+ // eslint-disable-next-line
69
+ // @ts-ignore
70
+ return this[command]?.();
71
+ }
72
+ return new Response('ok');
73
+ }
74
+ async reply(message) {
75
+ switch (this.update_type) {
76
+ case 'message': {
77
+ const request = new URL(this.api + '/sendMessage');
78
+ const params = new URLSearchParams();
79
+ params.append('chat_id', this.update.message?.chat.id.toString() ?? '');
80
+ params.append('reply_to_message_id', this.update.message?.message_id.toString() ?? '');
81
+ params.append('text', message);
82
+ console.log(`${request}?${params}`);
83
+ await fetch(`${request}?${params}`);
84
+ break;
85
+ }
86
+ case 'inline': {
87
+ const inline_request = new URL(this.api + '/answerInlineQuery');
88
+ const inline_params = new URLSearchParams();
89
+ inline_params.append('inline_query_id', this.update.inline_query?.id.toString() ?? '');
90
+ inline_params.append('results', JSON.stringify([new TelegramInlineQueryResultArticle(message)]));
91
+ console.log(`${inline_request}?${inline_params}`);
92
+ await fetch(`${inline_request}?${inline_params}`);
93
+ break;
94
+ }
95
+ default:
96
+ break;
97
+ }
98
+ }
99
+ }
100
+ class Webhook {
101
+ api;
102
+ webhook;
103
+ constructor(token, request) {
104
+ this.api = new URL('https://api.telegram.org/bot' + token);
105
+ this.webhook = new URL(new URL(request.url).origin + `/${token}`);
106
+ }
107
+ async set() {
108
+ const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
109
+ const params = url.searchParams;
110
+ params.append('url', this.webhook.toString());
111
+ params.append('max_connections', '100');
112
+ params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
113
+ params.append('drop_pending_updates', 'true');
114
+ return await fetch(`${url}?${params}`);
81
115
  }
82
116
  }
@@ -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,80 @@
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 fetch('https://clients5.google.com/translate_a/t?client=at&sl=auto&tl=en&q=' +
66
+ encodeURIComponent(bot3.update.inline_query?.query.toString() ?? ''))
67
+ .then((r) => r.json())
68
+ .then((json) => json[0].slice(0, -1).join(' '));
69
+ await bot3.reply(translated_text ?? '');
70
+ break;
71
+ }
72
+ default:
73
+ break;
74
+ }
75
+ return new Response('ok');
76
+ })
77
+ .handle(request.clone());
78
+ return new Response('ok');
79
+ },
94
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "6.6.0",
3
+ "version": "7.4.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",
@@ -35,11 +35,11 @@
35
35
  "@typescript-eslint/parser": "^7.8.0",
36
36
  "eslint": "^9.2.0",
37
37
  "eslint-config-prettier": "^9.1.0",
38
- "globals": "^15.1.0",
38
+ "globals": "^15.2.0",
39
39
  "lerna": "^8.1.2",
40
40
  "prettier": "^3.2.5",
41
41
  "typescript": "^5.4.5",
42
42
  "typescript-eslint": "^7.8.0"
43
43
  },
44
- "gitHead": "ac79b5c72e1c5379d512ea35dff333c0aca0ff2c"
44
+ "gitHead": "5a413513b05d2cde80b76d4e901dbe334e95ed25"
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;