@codebam/cf-workers-telegram-bot 7.4.0 → 7.7.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 (72) hide show
  1. package/dist/api.d.ts +14 -0
  2. package/dist/api.js +21 -0
  3. package/dist/ctx.d.ts +16 -0
  4. package/dist/ctx.js +56 -0
  5. package/dist/main.d.ts +4 -0
  6. package/dist/main.js +4 -0
  7. package/dist/telegram_bot.d.ts +16 -0
  8. package/dist/telegram_bot.js +74 -0
  9. package/dist/types/SerializableData.d.ts +2 -0
  10. package/dist/types/TelegramUser.js +1 -0
  11. package/dist/{main/src/types.d.ts → types.d.ts} +2 -1
  12. package/dist/webhook.d.ts +7 -0
  13. package/dist/webhook.js +17 -0
  14. package/package.json +45 -43
  15. package/LICENSE_MIT +0 -21
  16. package/dist/main/src/main.d.ts +0 -4
  17. package/dist/main/src/main.js +0 -4
  18. package/dist/main/src/telegram_bot.d.ts +0 -20
  19. package/dist/main/src/telegram_bot.js +0 -116
  20. package/dist/worker/src/worker.d.ts +0 -18
  21. package/dist/worker/src/worker.js +0 -80
  22. /package/dist/{main/src/types → types}/Balance.d.ts +0 -0
  23. /package/dist/{main/src/types → types}/Balance.js +0 -0
  24. /package/dist/{main/src/types → types}/Bored.d.ts +0 -0
  25. /package/dist/{main/src/types → types}/Bored.js +0 -0
  26. /package/dist/{main/src/types → types}/ChatPermissions.d.ts +0 -0
  27. /package/dist/{main/src/types → types}/ChatPermissions.js +0 -0
  28. /package/dist/{main/src/types → types}/DDGQueryResponse.d.ts +0 -0
  29. /package/dist/{main/src/types → types}/DDGQueryResponse.js +0 -0
  30. /package/dist/{main/src/types → types}/Joke.d.ts +0 -0
  31. /package/dist/{main/src/types → types}/Joke.js +0 -0
  32. /package/dist/{main/src/types → types}/Kanye.d.ts +0 -0
  33. /package/dist/{main/src/types → types}/Kanye.js +0 -0
  34. /package/dist/{main/src/types → types}/Kv.d.ts +0 -0
  35. /package/dist/{main/src/types → types}/Kv.js +0 -0
  36. /package/dist/{main/src/types → types}/PartialTelegramUpdate.d.ts +0 -0
  37. /package/dist/{main/src/types → types}/PartialTelegramUpdate.js +0 -0
  38. /package/dist/{main/src/types/TelegramChat.js → types/SerializableData.js} +0 -0
  39. /package/dist/{main/src/types → types}/TelegramChat.d.ts +0 -0
  40. /package/dist/{main/src/types/TelegramCommand.js → types/TelegramChat.js} +0 -0
  41. /package/dist/{main/src/types → types}/TelegramCommand.d.ts +0 -0
  42. /package/dist/{main/src/types/TelegramFrom.js → types/TelegramCommand.js} +0 -0
  43. /package/dist/{main/src/types → types}/TelegramFrom.d.ts +0 -0
  44. /package/dist/{main/src/types/TelegramInlineQuery.js → types/TelegramFrom.js} +0 -0
  45. /package/dist/{main/src/types → types}/TelegramInlineQuery.d.ts +0 -0
  46. /package/dist/{main/src/types/TelegramInlineQueryType.js → types/TelegramInlineQuery.js} +0 -0
  47. /package/dist/{main/src/types → types}/TelegramInlineQueryResult.d.ts +0 -0
  48. /package/dist/{main/src/types → types}/TelegramInlineQueryResult.js +0 -0
  49. /package/dist/{main/src/types → types}/TelegramInlineQueryResultArticle.d.ts +0 -0
  50. /package/dist/{main/src/types → types}/TelegramInlineQueryResultArticle.js +0 -0
  51. /package/dist/{main/src/types → types}/TelegramInlineQueryResultPhoto.d.ts +0 -0
  52. /package/dist/{main/src/types → types}/TelegramInlineQueryResultPhoto.js +0 -0
  53. /package/dist/{main/src/types → types}/TelegramInlineQueryType.d.ts +0 -0
  54. /package/dist/{main/src/types/TelegramInputMessageContent.js → types/TelegramInlineQueryType.js} +0 -0
  55. /package/dist/{main/src/types → types}/TelegramInputMessageContent.d.ts +0 -0
  56. /package/dist/{main/src/types/TelegramMessage.js → types/TelegramInputMessageContent.js} +0 -0
  57. /package/dist/{main/src/types → types}/TelegramMessage.d.ts +0 -0
  58. /package/dist/{main/src/types/TelegramMessageEntity.js → types/TelegramMessage.js} +0 -0
  59. /package/dist/{main/src/types → types}/TelegramMessageEntity.d.ts +0 -0
  60. /package/dist/{main/src/types/TelegramPhotoSize.js → types/TelegramMessageEntity.js} +0 -0
  61. /package/dist/{main/src/types → types}/TelegramPhotoSize.d.ts +0 -0
  62. /package/dist/{main/src/types/TelegramUser.js → types/TelegramPhotoSize.js} +0 -0
  63. /package/dist/{main/src/types → types}/TelegramUpdate.d.ts +0 -0
  64. /package/dist/{main/src/types → types}/TelegramUpdate.js +0 -0
  65. /package/dist/{main/src/types → types}/TelegramUser.d.ts +0 -0
  66. /package/dist/{main/src/types → types}/Update.d.ts +0 -0
  67. /package/dist/{main/src/types → types}/Update.js +0 -0
  68. /package/dist/{main/src/types → types}/WebhookCommands.d.ts +0 -0
  69. /package/dist/{main/src/types → types}/WebhookCommands.js +0 -0
  70. /package/dist/{main/src/types → types}/localhost.d.ts +0 -0
  71. /package/dist/{main/src/types → types}/localhost.js +0 -0
  72. /package/dist/{main/src/types.js → types.js} +0 -0
package/dist/api.d.ts ADDED
@@ -0,0 +1,14 @@
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/api.js ADDED
@@ -0,0 +1,21 @@
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
+ }
package/dist/ctx.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ import TelegramBot from "./telegram_bot";
3
+ import { SerializableData, TelegramUpdate } from "./types";
4
+ export default class ExecutionContext {
5
+ bot: TelegramBot;
6
+ update: TelegramUpdate;
7
+ update_type: string;
8
+ private data;
9
+ constructor(bot: TelegramBot, update: TelegramUpdate);
10
+ getText(): string;
11
+ next(): Response;
12
+ setData(key: string, value: SerializableData): this;
13
+ deleteData(key: string): this;
14
+ getData(key: string): SerializableData;
15
+ reply(message: string): Promise<void>;
16
+ }
package/dist/ctx.js ADDED
@@ -0,0 +1,56 @@
1
+ import API from "./api";
2
+ import { TelegramInlineQueryResultArticle } from "./types";
3
+ export default class ExecutionContext {
4
+ bot;
5
+ update;
6
+ update_type = '';
7
+ data = {};
8
+ constructor(bot, update) {
9
+ this.bot = bot;
10
+ this.update = update;
11
+ if (this.update.message?.text) {
12
+ this.update_type = 'message';
13
+ }
14
+ else if (this.update.inline_query?.query) {
15
+ this.update_type = 'inline';
16
+ }
17
+ }
18
+ getText() {
19
+ return this.update.message?.text || this.update.inline_query?.query || "";
20
+ }
21
+ next() {
22
+ return new Response('ok');
23
+ }
24
+ setData(key, value) {
25
+ this.data[key] = value;
26
+ return this;
27
+ }
28
+ deleteData(key) {
29
+ delete this.data[key];
30
+ return this;
31
+ }
32
+ getData(key) {
33
+ return this.data[key];
34
+ }
35
+ async reply(message) {
36
+ switch (this.update_type) {
37
+ case 'message': {
38
+ await API.sendMessage(this.bot.api.toString(), {
39
+ chat_id: this.update.message?.chat.id.toString() ?? '',
40
+ reply_to_message_id: this.update.message?.message_id.toString() ?? '',
41
+ text: message
42
+ });
43
+ break;
44
+ }
45
+ case 'inline': {
46
+ await API.answerInline(this.bot.api.toString(), {
47
+ inline_query_id: this.update.inline_query?.id.toString() ?? '',
48
+ results: [new TelegramInlineQueryResultArticle(message)]
49
+ });
50
+ break;
51
+ }
52
+ default:
53
+ break;
54
+ }
55
+ }
56
+ }
package/dist/main.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import ExecutionContext from './ctx';
2
+ import TelegramBot from './telegram_bot';
3
+ import * as Types from './types';
4
+ export { TelegramBot, Types, ExecutionContext };
package/dist/main.js ADDED
@@ -0,0 +1,4 @@
1
+ import ExecutionContext from './ctx';
2
+ import TelegramBot from './telegram_bot';
3
+ import * as Types from './types';
4
+ export { TelegramBot, Types, ExecutionContext };
@@ -0,0 +1,16 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ import { TelegramUpdate } from './types';
3
+ import ExecutionContext from './ctx';
4
+ import Webhook from './webhook';
5
+ export default class TelegramBot {
6
+ token: string;
7
+ webhook: Webhook;
8
+ api: URL;
9
+ update: TelegramUpdate;
10
+ update_type: string;
11
+ commands: Record<string, (ctx: ExecutionContext) => Promise<Response>>;
12
+ currentContext: ExecutionContext;
13
+ constructor(token: string);
14
+ on(event: string, callback: (ctx: ExecutionContext) => Promise<Response>): this;
15
+ handle(request: Request): Promise<Response>;
16
+ }
@@ -0,0 +1,74 @@
1
+ import { TelegramUpdate } from './types';
2
+ import ExecutionContext from './ctx';
3
+ import Webhook from './webhook';
4
+ export default class TelegramBot {
5
+ token;
6
+ webhook;
7
+ api;
8
+ update;
9
+ update_type;
10
+ commands = {};
11
+ currentContext;
12
+ constructor(token) {
13
+ this.token = token;
14
+ this.webhook = new Webhook('', new Request('http://127.0.0.1'));
15
+ this.api = new URL('https://api.telegram.org/bot' + token);
16
+ this.update = new TelegramUpdate({});
17
+ this.update_type = '';
18
+ }
19
+ on(event, callback) {
20
+ if (event !== 'on') {
21
+ // eslint-disable-next-line
22
+ // @ts-ignore TS7053
23
+ this.commands[event] = callback;
24
+ }
25
+ return this;
26
+ }
27
+ async handle(request) {
28
+ this.webhook = new Webhook(this.token, request);
29
+ if (request.method === 'POST') {
30
+ this.update = await request.json();
31
+ }
32
+ else {
33
+ this.update = new TelegramUpdate({});
34
+ }
35
+ const url = new URL(request.url);
36
+ if (`/${this.token}` === url.pathname) {
37
+ switch (url.searchParams.get('command')) {
38
+ case 'set':
39
+ return this.webhook.set();
40
+ default:
41
+ break;
42
+ }
43
+ console.log(this.update);
44
+ let command = 'default';
45
+ let args = [];
46
+ const ctx = new ExecutionContext(this, this.update);
47
+ this.currentContext = ctx;
48
+ switch (ctx.update_type) {
49
+ case 'message': {
50
+ // @ts-expect-error already checked above
51
+ args = this.update.message.text.split(' ');
52
+ break;
53
+ }
54
+ case 'inline': {
55
+ // @ts-expect-error already checked above
56
+ args = this.update.inline_query.query.split(' ');
57
+ break;
58
+ }
59
+ default:
60
+ break;
61
+ }
62
+ if (args.at(0)?.startsWith('/')) {
63
+ // @ts-expect-error already checked above
64
+ command = args.at(0).slice(1);
65
+ }
66
+ this.commands['any']?.(ctx);
67
+ if (!this.commands[command]) {
68
+ command = 'default';
69
+ }
70
+ return await this.commands[command]?.(ctx);
71
+ }
72
+ return new Response('ok');
73
+ }
74
+ }
@@ -0,0 +1,2 @@
1
+ type SerializableData = number | string | boolean;
2
+ export default SerializableData;
@@ -0,0 +1 @@
1
+ export {};
@@ -23,4 +23,5 @@ import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultA
23
23
  import DDGQueryResponse from './types/DDGQueryResponse';
24
24
  import ChatPermissions from './types/ChatPermissions';
25
25
  import Kanye from './types/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, };
26
+ import SerializableData from './types/SerializableData';
27
+ 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, SerializableData, };
@@ -0,0 +1,7 @@
1
+ /// <reference types="@cloudflare/workers-types" />
2
+ export default class Webhook {
3
+ api: URL;
4
+ webhook: URL;
5
+ constructor(token: string, request: Request);
6
+ set(): Promise<Response>;
7
+ }
@@ -0,0 +1,17 @@
1
+ export default class Webhook {
2
+ api;
3
+ webhook;
4
+ constructor(token, request) {
5
+ this.api = new URL('https://api.telegram.org/bot' + token);
6
+ this.webhook = new URL(new URL(request.url).origin + `/${token}`);
7
+ }
8
+ async set() {
9
+ const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
10
+ const params = url.searchParams;
11
+ params.append('url', this.webhook.toString());
12
+ params.append('max_connections', '100');
13
+ params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
14
+ params.append('drop_pending_updates', 'true');
15
+ return await fetch(`${url}?${params}`);
16
+ }
17
+ }
package/package.json CHANGED
@@ -1,45 +1,47 @@
1
1
  {
2
- "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "7.4.0",
4
- "description": "serverless telegram bot on cf workers",
5
- "main": "./dist/main/src/main.js",
6
- "module": "./dist/main/src/main.js",
7
- "files": [
8
- "dist",
9
- "LICENSE",
10
- "LICENSE_MIT",
11
- "README.md"
12
- ],
13
- "keywords": [
14
- "cloudflare",
15
- "telegram",
16
- "worker",
17
- "webhook"
18
- ],
19
- "type": "module",
20
- "private": false,
21
- "scripts": {
22
- "build": "tsc --project tsconfig.json",
23
- "lint": "eslint src"
24
- },
25
- "author": "codebam",
26
- "license": "Apache-2.0",
27
- "repository": {
28
- "type": "git",
29
- "url": "https://github.com/codebam/cf-workers-telegram-bot.git"
30
- },
31
- "devDependencies": {
32
- "@cloudflare/workers-types": "^4.20240502.0",
33
- "@eslint/js": "^9.2.0",
34
- "@typescript-eslint/eslint-plugin": "^7.8.0",
35
- "@typescript-eslint/parser": "^7.8.0",
36
- "eslint": "^9.2.0",
37
- "eslint-config-prettier": "^9.1.0",
38
- "globals": "^15.2.0",
39
- "lerna": "^8.1.2",
40
- "prettier": "^3.2.5",
41
- "typescript": "^5.4.5",
42
- "typescript-eslint": "^7.8.0"
43
- },
44
- "gitHead": "5a413513b05d2cde80b76d4e901dbe334e95ed25"
2
+ "name": "@codebam/cf-workers-telegram-bot",
3
+ "version": "7.7.0",
4
+ "description": "serverless telegram bot on cf workers",
5
+ "main": "./dist/main/src/main.js",
6
+ "module": "./dist/main/src/main.js",
7
+ "files": [
8
+ "dist",
9
+ "LICENSE",
10
+ "LICENSE_MIT",
11
+ "README.md"
12
+ ],
13
+ "keywords": [
14
+ "cloudflare",
15
+ "telegram",
16
+ "worker",
17
+ "webhook"
18
+ ],
19
+ "type": "module",
20
+ "private": false,
21
+ "scripts": {
22
+ "build": "tsc --project tsconfig.json",
23
+ "lint": "eslint src",
24
+ "test": "vitest --config vitest.config.js"
25
+ },
26
+ "author": "codebam",
27
+ "license": "Apache-2.0",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/codebam/cf-workers-telegram-bot.git"
31
+ },
32
+ "devDependencies": {
33
+ "@cloudflare/workers-types": "^4.20240502.0",
34
+ "@eslint/js": "^9.2.0",
35
+ "@typescript-eslint/eslint-plugin": "^7.8.0",
36
+ "@typescript-eslint/parser": "^7.8.0",
37
+ "eslint": "^9.2.0",
38
+ "eslint-config-prettier": "^9.1.0",
39
+ "globals": "^15.2.0",
40
+ "lerna": "^8.1.2",
41
+ "prettier": "^3.2.5",
42
+ "typescript": "^5.4.5",
43
+ "typescript-eslint": "^7.8.0",
44
+ "vitest": "^1.6.0"
45
+ },
46
+ "gitHead": "11d5f60c49614aaeaa1e980790be6c2c3ef2057a"
45
47
  }
package/LICENSE_MIT DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Nikhil John
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,4 +0,0 @@
1
- import TelegramBot from './telegram_bot';
2
- import * as Types from './types';
3
- import worker from '../../worker/src/worker';
4
- export { TelegramBot, Types, worker };
@@ -1,4 +0,0 @@
1
- import TelegramBot from './telegram_bot';
2
- import * as Types from './types';
3
- import worker from '../../worker/src/worker';
4
- export { TelegramBot, Types, worker };
@@ -1,20 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
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>;
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,116 +0,0 @@
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}`);
115
- }
116
- }
@@ -1,18 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- interface Environment {
3
- SECRET_TELEGRAM_API_TOKEN: string;
4
- KV_GET_SET: KVNamespace;
5
- KV_UID_DATA: KVNamespace;
6
- SECRET_TELEGRAM_API_TOKEN2: string;
7
- SECRET_TELEGRAM_API_TOKEN3: string;
8
- SECRET_TELEGRAM_API_TOKEN4: string;
9
- SECRET_TELEGRAM_API_TOKEN5: string;
10
- AI: Ai;
11
- DB: D1Database;
12
- R2: R2Bucket;
13
- CHAT_MODEL: string;
14
- }
15
- declare const _default: {
16
- fetch: (request: Request, env: Environment) => Promise<Response>;
17
- };
18
- export default _default;
@@ -1,80 +0,0 @@
1
- import TelegramBot from '../../main/src/telegram_bot';
2
- export default {
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
- },
80
- };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes