@codebam/cf-workers-telegram-bot 6.1.0 → 6.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.
package/README.md CHANGED
@@ -1,12 +1,20 @@
1
1
  # cf-workers-telegram-bot
2
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)
4
+
5
+ ![screenshot of cf-workers-telegram-bot](/screenshot.png)
6
+
3
7
  serverless telegram bot on cf workers
4
8
 
5
9
  The original `worker.js` is the content of Nikhil John's
6
10
  https://github.com/nikhiljohn10/telegram-bot-worker which is licensed with MIT.
7
11
  My modifications are licensed under the Apache license.
8
12
 
9
- [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/codebam/cf-workers-telegram-bot)
13
+ The first bot configuration is fully featured, and responds to all received
14
+ messages with llama2 if a command isn't found.
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
10
18
 
11
19
  To use the deploy button:
12
20
 
@@ -14,8 +22,8 @@ To use the deploy button:
14
22
  - Navigate to your new **GitHub repository > Settings > Secrets** and add the following secrets:
15
23
 
16
24
  ```yaml
17
- - Name: CF_API_TOKEN (should be added automatically)
18
- - Name: CF_ACCOUNT_ID (should be added automatically)
25
+ - Name: CLOUDFLARE_API_TOKEN (should be added automatically)
26
+ - Name: CLOUDFLARE_ACCOUNT_ID (should be added automatically)
19
27
 
20
28
  - Name: SECRET_TELEGRAM_API_TOKEN
21
29
  - Value: your-telegram-bot-token
@@ -26,9 +34,13 @@ To use the deploy button:
26
34
  To fork this repo and use wrangler:
27
35
 
28
36
  - Click fork
37
+ - `npm i -g wrangler`
29
38
  - `wrangler secret put SECRET_TELEGRAM_API_TOKEN` and set it to your telegram
30
39
  bot token
31
- - `wrangler publish`
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`
32
44
  - Done!
33
45
 
34
46
  ## Getting started with cf-workers-telegram-bot
@@ -36,10 +48,10 @@ To fork this repo and use wrangler:
36
48
  Once you've deployed the bot you can get your Webhook command URL by doing any
37
49
  of the following.
38
50
 
39
- - sha256sum(YourTelegramSecretKey) is the path to your webhook commands and
51
+ - sha256sum(SECRET_TELEGRAM_API_TOKEN) is the path to your webhook commands and
40
52
  should be put at the end of your worker URL to access commands such as
41
53
  setting your webhook
42
- - Use `sha256sum <<< "your secret key"` to get the path
54
+ - Use `echo -n yoursecretkey | sha256sum` to get the path
43
55
  - Open the Cloudflare Worker Logs under **Workers &gt; cf-workers-telegram-bot
44
56
  &gt; Logs &gt; Begin log stream** and make a GET request (open it in your browser)
45
57
  to your Worker URL and look at the logs to see your Access URL
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1,8 @@
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
+ };
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1,8 @@
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
+ };
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1 @@
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,6 +1,6 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import BotApi from './bot_api';
3
- import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update } from './types';
3
+ import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update, ChatPermissions } from './types';
4
4
  import Handler from './handler';
5
5
  export default class TelegramApi extends BotApi {
6
6
  constructor(commands: Commands, webhook: Webhook, handler: Handler);
@@ -29,4 +29,6 @@ export default class TelegramApi extends BotApi {
29
29
  sendPoll: (chat_id: number, question: string, options: string[], is_anonymous?: boolean, type?: string, allows_multiple_answers?: boolean, correct_option_id?: number, explanation?: string, explanation_parse_mode?: string, open_period?: number, close_date?: number, is_closed?: boolean, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
30
30
  sendDice: (chat_id: number, emoji?: string, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
31
31
  getUserProfilePhotos: (user_id: number, offset?: number, limit?: number) => Promise<Response>;
32
+ banChatMember: (chat_id: number | string, user_id: number, until_date?: number, revoke_messages?: boolean) => Promise<Response>;
33
+ restrictChatMember: (chat_id: number | string, user_id: number, permissions: ChatPermissions, use_independent_chat_permissions: boolean, until_date: number) => Promise<Response>;
32
34
  }
@@ -170,4 +170,17 @@ export default class TelegramApi extends BotApi {
170
170
  offset: offset.toString(),
171
171
  limit: limit.toString(),
172
172
  }).href));
173
+ banChatMember = async (chat_id, user_id, until_date = 0, revoke_messages = false) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/banChatMember`), {
174
+ chat_id: chat_id.toString(),
175
+ user_id: user_id.toString(),
176
+ until_date: until_date.toString(),
177
+ revoke_messages: revoke_messages.toString(),
178
+ }).href));
179
+ restrictChatMember = async (chat_id, user_id, permissions, use_independent_chat_permissions, until_date) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/restrictChatMember`), {
180
+ chat_id: chat_id.toString(),
181
+ user_id: user_id.toString(),
182
+ permissions: JSON.stringify(permissions),
183
+ use_independent_chat_permissions: use_independent_chat_permissions.toString(),
184
+ until_date: until_date.toString(),
185
+ }).href));
173
186
  }
@@ -19,6 +19,9 @@ export default class TelegramBot extends TelegramApi {
19
19
  toss: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
20
20
  ping: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
21
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>;
22
25
  url: URL;
23
26
  kv: Kv;
24
27
  get_set: KVNamespace;
@@ -16,6 +16,9 @@ import commandlist from './commands/commandlist';
16
16
  import toss from './commands/toss';
17
17
  import ping from './commands/ping';
18
18
  import getchatinfo from './commands/getchatinfo';
19
+ import start from './commands/start';
20
+ import ban from './commands/ban';
21
+ import mute from './commands/mute';
19
22
  export default class TelegramBot extends TelegramApi {
20
23
  translate;
21
24
  clear;
@@ -34,6 +37,9 @@ export default class TelegramBot extends TelegramApi {
34
37
  toss;
35
38
  ping;
36
39
  getChatInfo;
40
+ start;
41
+ ban;
42
+ mute;
37
43
  url;
38
44
  kv;
39
45
  get_set;
@@ -61,6 +67,9 @@ export default class TelegramBot extends TelegramApi {
61
67
  this.toss = toss;
62
68
  this.ping = ping;
63
69
  this.getChatInfo = getchatinfo;
70
+ this.start = start;
71
+ this.ban = ban;
72
+ this.mute = mute;
64
73
  this.url = config.url;
65
74
  this.kv = config.kv;
66
75
  this.get_set = config.kv?.get_set;
@@ -16,4 +16,7 @@ export default class TelegramCommands {
16
16
  static commandList: TelegramCommand;
17
17
  static image: TelegramCommand;
18
18
  static translate: TelegramCommand;
19
+ static start: TelegramCommand;
20
+ static ban: TelegramCommand;
21
+ static mute: TelegramCommand;
19
22
  }
@@ -15,4 +15,7 @@ export default class TelegramCommands {
15
15
  static commandList = async (bot, update) => bot.commandList(bot, update);
16
16
  static image = async (bot, update, args) => bot.image(bot, update, args);
17
17
  static translate = async (bot, update, args) => bot.translate(bot, update, args);
18
+ static start = async (bot, update) => bot.start(bot, update);
19
+ static ban = async (bot, update) => bot.ban(bot, update);
20
+ static mute = async (bot, update) => bot.mute(bot, update);
18
21
  }
@@ -0,0 +1,17 @@
1
+ type ChatPermissions = {
2
+ can_send_messages?: boolean;
3
+ can_send_audios?: boolean;
4
+ can_send_documents?: boolean;
5
+ can_send_photos?: boolean;
6
+ can_send_videos?: boolean;
7
+ can_send_video_notes?: boolean;
8
+ can_send_voice_notes?: boolean;
9
+ can_send_polls?: boolean;
10
+ can_send_other_messages?: boolean;
11
+ can_add_web_page_previews?: boolean;
12
+ can_change_info?: boolean;
13
+ can_invite_users?: boolean;
14
+ can_pin_messages?: boolean;
15
+ can_manage_topics?: boolean;
16
+ };
17
+ export default ChatPermissions;
@@ -0,0 +1 @@
1
+ export {};
@@ -25,4 +25,5 @@ import TelegramInlineQueryResult from './types/TelegramInlineQueryResult';
25
25
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto';
26
26
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle';
27
27
  import DDGQueryResponse from './types/DDGQueryResponse';
28
- 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, };
28
+ import ChatPermissions from './types/ChatPermissions';
29
+ 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, };
@@ -23,7 +23,9 @@ export default {
23
23
  '/clear': TelegramCommands.clear,
24
24
  '/help': TelegramCommands.commandList,
25
25
  '/image': TelegramCommands.image,
26
- '/start': TelegramCommands.question,
26
+ '/start': TelegramCommands.start,
27
+ '/ban': TelegramCommands.ban,
28
+ '/mute': TelegramCommands.mute,
27
29
  },
28
30
  kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
29
31
  ai: env.AI,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "6.1.0",
3
+ "version": "6.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",
@@ -41,5 +41,5 @@
41
41
  "typescript": "^5.4.5",
42
42
  "typescript-eslint": "^7.8.0"
43
43
  },
44
- "gitHead": "0f3db27086c56ef9d50370c217ed09b5d5b3d401"
44
+ "gitHead": "3908a7cd0ca117b7e641960bb8a20cadaddc307e"
45
45
  }