@codebam/cf-workers-telegram-bot 7.20.0 → 7.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/dist/main.d.ts +1 -9
  2. package/dist/main.js +1 -2
  3. package/dist/telegram_api.d.ts +35 -5
  4. package/dist/telegram_api.js +33 -0
  5. package/dist/telegram_bot.d.ts +20 -0
  6. package/dist/telegram_bot.js +20 -0
  7. package/dist/telegram_execution_context.d.ts +34 -9
  8. package/dist/telegram_execution_context.js +31 -14
  9. package/dist/types.d.ts +1 -9
  10. package/dist/types.js +1 -2
  11. package/package.json +1 -1
  12. package/dist/api.d.ts +0 -30
  13. package/dist/api.js +0 -37
  14. package/dist/ctx.d.ts +0 -19
  15. package/dist/ctx.js +0 -109
  16. package/dist/main/src/TelegramBot.1.d.ts +0 -0
  17. package/dist/main/src/TelegramBot.1.js +0 -1
  18. package/dist/main/src/TelegramBot.d.ts +0 -0
  19. package/dist/main/src/TelegramBot.js +0 -1
  20. package/dist/main/src/api.d.ts +0 -14
  21. package/dist/main/src/api.js +0 -21
  22. package/dist/main/src/bot_api.d.ts +0 -10
  23. package/dist/main/src/bot_api.js +0 -11
  24. package/dist/main/src/commands/ban.d.ts +0 -5
  25. package/dist/main/src/commands/ban.js +0 -8
  26. package/dist/main/src/commands/bored.d.ts +0 -5
  27. package/dist/main/src/commands/bored.js +0 -7
  28. package/dist/main/src/commands/clear.d.ts +0 -5
  29. package/dist/main/src/commands/clear.js +0 -14
  30. package/dist/main/src/commands/code.d.ts +0 -5
  31. package/dist/main/src/commands/code.js +0 -4
  32. package/dist/main/src/commands/commandlist.d.ts +0 -5
  33. package/dist/main/src/commands/commandlist.js +0 -1
  34. package/dist/main/src/commands/dog.d.ts +0 -5
  35. package/dist/main/src/commands/dog.js +0 -7
  36. package/dist/main/src/commands/duckduckgo.d.ts +0 -5
  37. package/dist/main/src/commands/duckduckgo.js +0 -42
  38. package/dist/main/src/commands/epoch.d.ts +0 -5
  39. package/dist/main/src/commands/epoch.js +0 -4
  40. package/dist/main/src/commands/getchatinfo.d.ts +0 -5
  41. package/dist/main/src/commands/getchatinfo.js +0 -1
  42. package/dist/main/src/commands/image.d.ts +0 -5
  43. package/dist/main/src/commands/image.js +0 -19
  44. package/dist/main/src/commands/joke.d.ts +0 -5
  45. package/dist/main/src/commands/joke.js +0 -7
  46. package/dist/main/src/commands/kanye.d.ts +0 -5
  47. package/dist/main/src/commands/kanye.js +0 -8
  48. package/dist/main/src/commands/mute.d.ts +0 -5
  49. package/dist/main/src/commands/mute.js +0 -8
  50. package/dist/main/src/commands/ping.d.ts +0 -5
  51. package/dist/main/src/commands/ping.js +0 -1
  52. package/dist/main/src/commands/question.d.ts +0 -5
  53. package/dist/main/src/commands/question.js +0 -74
  54. package/dist/main/src/commands/roll.d.ts +0 -5
  55. package/dist/main/src/commands/roll.js +0 -6
  56. package/dist/main/src/commands/sean.d.ts +0 -5
  57. package/dist/main/src/commands/sean.js +0 -76
  58. package/dist/main/src/commands/start.d.ts +0 -5
  59. package/dist/main/src/commands/start.js +0 -1
  60. package/dist/main/src/commands/toss.d.ts +0 -5
  61. package/dist/main/src/commands/toss.js +0 -1
  62. package/dist/main/src/commands/translate.d.ts +0 -5
  63. package/dist/main/src/commands/translate.js +0 -26
  64. package/dist/main/src/handler.d.ts +0 -12
  65. package/dist/main/src/handler.js +0 -40
  66. package/dist/main/src/libs.d.ts +0 -5
  67. package/dist/main/src/libs.js +0 -7
  68. package/dist/main/src/main.d.ts +0 -4
  69. package/dist/main/src/main.js +0 -4
  70. package/dist/main/src/telegram_api.d.ts +0 -34
  71. package/dist/main/src/telegram_api.js +0 -192
  72. package/dist/main/src/telegram_bot.d.ts +0 -20
  73. package/dist/main/src/telegram_bot.js +0 -116
  74. package/dist/main/src/telegram_commands.d.ts +0 -22
  75. package/dist/main/src/telegram_commands.js +0 -21
  76. package/dist/main/src/telegram_webhook.d.ts +0 -10
  77. package/dist/main/src/telegram_webhook.js +0 -20
  78. package/dist/main/src/types/Balance.d.ts +0 -6
  79. package/dist/main/src/types/Balance.js +0 -1
  80. package/dist/main/src/types/Bored.d.ts +0 -10
  81. package/dist/main/src/types/Bored.js +0 -1
  82. package/dist/main/src/types/ChatPermissions.d.ts +0 -17
  83. package/dist/main/src/types/ChatPermissions.js +0 -1
  84. package/dist/main/src/types/Command.d.ts +0 -5
  85. package/dist/main/src/types/Command.js +0 -1
  86. package/dist/main/src/types/CommandArgs.d.ts +0 -5
  87. package/dist/main/src/types/CommandArgs.js +0 -1
  88. package/dist/main/src/types/Commands.d.ts +0 -3
  89. package/dist/main/src/types/Commands.js +0 -1
  90. package/dist/main/src/types/Config.d.ts +0 -20
  91. package/dist/main/src/types/Config.js +0 -33
  92. package/dist/main/src/types/DDGQueryResponse.d.ts +0 -12
  93. package/dist/main/src/types/DDGQueryResponse.js +0 -1
  94. package/dist/main/src/types/DataTypes.d.ts +0 -2
  95. package/dist/main/src/types/DataTypes.js +0 -1
  96. package/dist/main/src/types/Joke.d.ts +0 -20
  97. package/dist/main/src/types/Joke.js +0 -1
  98. package/dist/main/src/types/Kanye.d.ts +0 -4
  99. package/dist/main/src/types/Kanye.js +0 -1
  100. package/dist/main/src/types/Kv.d.ts +0 -3
  101. package/dist/main/src/types/Kv.js +0 -1
  102. package/dist/main/src/types/PartialTelegramUpdate.d.ts +0 -11
  103. package/dist/main/src/types/PartialTelegramUpdate.js +0 -1
  104. package/dist/main/src/types/TelegramChat.d.ts +0 -21
  105. package/dist/main/src/types/TelegramChat.js +0 -1
  106. package/dist/main/src/types/TelegramCommand.d.ts +0 -5
  107. package/dist/main/src/types/TelegramCommand.js +0 -1
  108. package/dist/main/src/types/TelegramFrom.d.ts +0 -8
  109. package/dist/main/src/types/TelegramFrom.js +0 -1
  110. package/dist/main/src/types/TelegramInlineQuery.d.ts +0 -9
  111. package/dist/main/src/types/TelegramInlineQuery.js +0 -1
  112. package/dist/main/src/types/TelegramInlineQueryResult.d.ts +0 -6
  113. package/dist/main/src/types/TelegramInlineQueryResult.js +0 -8
  114. package/dist/main/src/types/TelegramInlineQueryResultArticle.d.ts +0 -8
  115. package/dist/main/src/types/TelegramInlineQueryResultArticle.js +0 -15
  116. package/dist/main/src/types/TelegramInlineQueryResultPhoto.d.ts +0 -13
  117. package/dist/main/src/types/TelegramInlineQueryResultPhoto.js +0 -19
  118. package/dist/main/src/types/TelegramInlineQueryType.d.ts +0 -2
  119. package/dist/main/src/types/TelegramInlineQueryType.js +0 -1
  120. package/dist/main/src/types/TelegramInputMessageContent.d.ts +0 -5
  121. package/dist/main/src/types/TelegramInputMessageContent.js +0 -1
  122. package/dist/main/src/types/TelegramMessage.d.ts +0 -43
  123. package/dist/main/src/types/TelegramMessage.js +0 -1
  124. package/dist/main/src/types/TelegramMessageEntity.d.ts +0 -10
  125. package/dist/main/src/types/TelegramMessageEntity.js +0 -1
  126. package/dist/main/src/types/TelegramPhotoSize.d.ts +0 -8
  127. package/dist/main/src/types/TelegramPhotoSize.js +0 -1
  128. package/dist/main/src/types/TelegramUpdate.d.ts +0 -13
  129. package/dist/main/src/types/TelegramUpdate.js +0 -36
  130. package/dist/main/src/types/TelegramUser.d.ts +0 -12
  131. package/dist/main/src/types/TelegramUser.js +0 -1
  132. package/dist/main/src/types/Update.d.ts +0 -3
  133. package/dist/main/src/types/Update.js +0 -2
  134. package/dist/main/src/types/WebhookCommands.d.ts +0 -4
  135. package/dist/main/src/types/WebhookCommands.js +0 -2
  136. package/dist/main/src/types/localhost.d.ts +0 -3
  137. package/dist/main/src/types/localhost.js +0 -1
  138. package/dist/main/src/types/types.d.ts +0 -1
  139. package/dist/main/src/types/types.js +0 -1
  140. package/dist/main/src/types.d.ts +0 -26
  141. package/dist/main/src/types.js +0 -8
  142. package/dist/main/src/webhook.d.ts +0 -10
  143. package/dist/main/src/webhook.js +0 -15
  144. package/dist/types/Balance.d.ts +0 -6
  145. package/dist/types/Balance.js +0 -1
  146. package/dist/types/Bored.d.ts +0 -10
  147. package/dist/types/Bored.js +0 -1
  148. package/dist/types/DDGQueryResponse.d.ts +0 -12
  149. package/dist/types/DDGQueryResponse.js +0 -1
  150. package/dist/types/Joke.d.ts +0 -20
  151. package/dist/types/Joke.js +0 -1
  152. package/dist/types/Kanye.d.ts +0 -4
  153. package/dist/types/Kanye.js +0 -1
  154. package/dist/types/Kv.d.ts +0 -3
  155. package/dist/types/Kv.js +0 -1
  156. package/dist/types/SerializableData.d.ts +0 -2
  157. package/dist/types/SerializableData.js +0 -1
  158. package/dist/types/localhost.d.ts +0 -3
  159. package/dist/types/localhost.js +0 -1
  160. package/dist/worker/src/worker.d.ts +0 -18
  161. package/dist/worker/src/worker.js +0 -81
package/dist/main.d.ts CHANGED
@@ -3,12 +3,7 @@ import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
5
  import TelegramCommand from './types/TelegramCommand.js';
6
- import Kv from './types/Kv.js';
7
- import localhost from './types/localhost.js';
8
6
  import WebhookCommands from './types/WebhookCommands.js';
9
- import Joke from './types/Joke.js';
10
- import Bored from './types/Bored.js';
11
- import Balance from './types/Balance.js';
12
7
  import TelegramFrom from './types/TelegramFrom.js';
13
8
  import TelegramChat from './types/TelegramChat.js';
14
9
  import TelegramUser from './types/TelegramUser.js';
@@ -24,9 +19,6 @@ import TelegramInlineQueryType from './types/TelegramInlineQueryType.js';
24
19
  import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
25
20
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
26
21
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
27
- import DDGQueryResponse from './types/DDGQueryResponse.js';
28
22
  import ChatPermissions from './types/ChatPermissions.js';
29
- import Kanye from './types/Kanye.js';
30
- import SerializableData from './types/SerializableData.js';
31
23
  export default TelegramBot;
32
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, 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, };
24
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramCommand, WebhookCommands, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, ChatPermissions, };
package/dist/main.js CHANGED
@@ -2,7 +2,6 @@ import TelegramBot from './telegram_bot.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
- import localhost from './types/localhost.js';
6
5
  import WebhookCommands from './types/WebhookCommands.js';
7
6
  import Update from './types/Update.js';
8
7
  import TelegramUpdate from './types/TelegramUpdate.js';
@@ -10,4 +9,4 @@ import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
10
9
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
11
10
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
12
11
  export default TelegramBot;
13
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, localhost, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
12
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
@@ -1,30 +1,60 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import SerializableData from './types/SerializableData.js';
3
2
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
4
3
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
5
4
  import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
5
+ /** Class representing the Telegram API and all it's methods */
6
6
  export default class TelegramApi {
7
- getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): Request<unknown, CfProperties<unknown>>;
8
- getFile(botApi: string, data: {
9
- file_id: string;
10
- }, token: string): Promise<Response>;
7
+ /**
8
+ * Get the API URL for a given bot API and slug
9
+ * @param botApi - full URL to the telegram API without slug
10
+ * @param slug - slug to append to the API URL
11
+ * @param data - data to append to the request
12
+ */
13
+ getApiUrl(botApi: string, slug: string, data: Record<string, number | string | boolean>): Request<unknown, CfProperties<unknown>>;
14
+ /**
15
+ * Get a file with a given file_id
16
+ * @param botApi - full URL to the telegram API without slug
17
+ * @param data - data to append to the request
18
+ * @param token - bot token
19
+ */
20
+ getFile(botApi: string, data: Record<string, number | string | boolean>, token: string): Promise<Response>;
21
+ /**
22
+ * Send a message to a given botApi
23
+ * @param botApi - full URL to the telegram API without slug
24
+ * @param data - data to append to the request
25
+ */
11
26
  sendMessage(botApi: string, data: {
12
27
  reply_to_message_id: number | string;
13
28
  chat_id: number | string;
14
29
  text: string;
15
30
  parse_mode: string;
16
31
  }): Promise<Response>;
32
+ /**
33
+ * Send a video message to a given botApi
34
+ * @param botApi - full URL to the telegram API without slug
35
+ * @param data - data to append to the request
36
+ */
17
37
  sendVideo(botApi: string, data: {
18
38
  reply_to_message_id: number | string;
19
39
  chat_id: number | string;
20
40
  video: string;
21
41
  }): Promise<Response>;
42
+ /**
43
+ * Send a photo message to a given botApi
44
+ * @param botApi - full URL to the telegram API without slug
45
+ * @param data - data to append to the request
46
+ */
22
47
  sendPhoto(botApi: string, data: {
23
48
  reply_to_message_id: number | string;
24
49
  chat_id: number | string;
25
50
  photo: string;
26
51
  caption: string;
27
52
  }): Promise<Response>;
53
+ /**
54
+ * Send an inline response to a given botApi
55
+ * @param botApi - full URL to the telegram API without slug
56
+ * @param data - data to append to the request
57
+ */
28
58
  answerInline(botApi: string, data: {
29
59
  inline_query_id: number | string;
30
60
  results: TelegramInlineQueryResultArticle[] | TelegramInlineQueryResultPhoto[] | TelegramInlineQueryResultVideo[];
@@ -1,4 +1,11 @@
1
+ /** Class representing the Telegram API and all it's methods */
1
2
  export default class TelegramApi {
3
+ /**
4
+ * Get the API URL for a given bot API and slug
5
+ * @param botApi - full URL to the telegram API without slug
6
+ * @param slug - slug to append to the API URL
7
+ * @param data - data to append to the request
8
+ */
2
9
  getApiUrl(botApi, slug, data) {
3
10
  const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
4
11
  const params = new URLSearchParams();
@@ -7,6 +14,12 @@ export default class TelegramApi {
7
14
  }
8
15
  return new Request(`${request.toString()}?${params.toString()}`);
9
16
  }
17
+ /**
18
+ * Get a file with a given file_id
19
+ * @param botApi - full URL to the telegram API without slug
20
+ * @param data - data to append to the request
21
+ * @param token - bot token
22
+ */
10
23
  async getFile(botApi, data, token) {
11
24
  const url = this.getApiUrl(botApi, 'getFile', data);
12
25
  const response = await fetch(url);
@@ -21,18 +34,38 @@ export default class TelegramApi {
21
34
  }
22
35
  return await fetch(`https://api.telegram.org/file/bot${token}/${file_path}`);
23
36
  }
37
+ /**
38
+ * Send a message to a given botApi
39
+ * @param botApi - full URL to the telegram API without slug
40
+ * @param data - data to append to the request
41
+ */
24
42
  async sendMessage(botApi, data) {
25
43
  const url = this.getApiUrl(botApi, 'sendMessage', data);
26
44
  return await fetch(url);
27
45
  }
46
+ /**
47
+ * Send a video message to a given botApi
48
+ * @param botApi - full URL to the telegram API without slug
49
+ * @param data - data to append to the request
50
+ */
28
51
  async sendVideo(botApi, data) {
29
52
  const url = this.getApiUrl(botApi, 'sendVideo', data);
30
53
  return await fetch(url);
31
54
  }
55
+ /**
56
+ * Send a photo message to a given botApi
57
+ * @param botApi - full URL to the telegram API without slug
58
+ * @param data - data to append to the request
59
+ */
32
60
  async sendPhoto(botApi, data) {
33
61
  const url = this.getApiUrl(botApi, 'sendPhoto', data);
34
62
  return await fetch(url);
35
63
  }
64
+ /**
65
+ * Send an inline response to a given botApi
66
+ * @param botApi - full URL to the telegram API without slug
67
+ * @param data - data to append to the request
68
+ */
36
69
  async answerInline(botApi, data) {
37
70
  const url = this.getApiUrl(botApi, 'answerInlineQuery', {
38
71
  inline_query_id: data.inline_query_id,
@@ -2,14 +2,34 @@
2
2
  import TelegramUpdate from './types/TelegramUpdate.js';
3
3
  import TelegramExecutionContext from './telegram_execution_context.js';
4
4
  import Webhook from './webhook.js';
5
+ /** Class representing a telegram bot. */
5
6
  export default class TelegramBot {
7
+ /** The telegram token */
6
8
  token: string;
9
+ /** The telegram api URL */
7
10
  api: URL;
11
+ /** The telegram webhook object */
8
12
  webhook: Webhook;
13
+ /** The telegram update object */
9
14
  update: TelegramUpdate;
15
+ /** The telegram commands record map */
10
16
  commands: Record<string, (ctx: TelegramExecutionContext) => Promise<Response>>;
17
+ /** The current bot context */
11
18
  currentContext: TelegramExecutionContext;
19
+ /**
20
+ * Create a bot
21
+ * @param token - the telegram secret token
22
+ */
12
23
  constructor(token: string);
24
+ /**
25
+ * Register a function on the bot
26
+ * @param event - the event or command name
27
+ * @param callback - the bot context
28
+ */
13
29
  on(event: string, callback: (ctx: TelegramExecutionContext) => Promise<Response>): this;
30
+ /**
31
+ * Handle a request on a given bot
32
+ * @param request - the request to handle
33
+ */
14
34
  handle(request: Request): Promise<Response>;
15
35
  }
@@ -1,23 +1,43 @@
1
1
  import TelegramUpdate from './types/TelegramUpdate.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
+ /** Class representing a telegram bot. */
4
5
  export default class TelegramBot {
6
+ /** The telegram token */
5
7
  token;
8
+ /** The telegram api URL */
6
9
  api;
10
+ /** The telegram webhook object */
7
11
  webhook = new Webhook('', new Request('http://127.0.0.1'));
12
+ /** The telegram update object */
8
13
  update = new TelegramUpdate({});
14
+ /** The telegram commands record map */
9
15
  commands = {};
16
+ /** The current bot context */
10
17
  currentContext;
18
+ /**
19
+ * Create a bot
20
+ * @param token - the telegram secret token
21
+ */
11
22
  constructor(token) {
12
23
  this.token = token;
13
24
  this.api = new URL('https://api.telegram.org/bot' + token);
14
25
  }
26
+ /**
27
+ * Register a function on the bot
28
+ * @param event - the event or command name
29
+ * @param callback - the bot context
30
+ */
15
31
  on(event, callback) {
16
32
  if (!['on', 'handle'].includes(event)) {
17
33
  this.commands[event] = callback;
18
34
  }
19
35
  return this;
20
36
  }
37
+ /**
38
+ * Handle a request on a given bot
39
+ * @param request - the request to handle
40
+ */
21
41
  async handle(request) {
22
42
  this.webhook = new Webhook(this.token, request);
23
43
  const url = new URL(request.url);
@@ -1,21 +1,46 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import TelegramApi from './telegram_api.js';
3
3
  import TelegramBot from './telegram_bot.js';
4
- import SerializableData from './types/SerializableData.js';
5
4
  import TelegramUpdate from './types/TelegramUpdate.js';
5
+ /** Class representing the context of execution */
6
6
  export default class TelegramExecutionContext {
7
+ /** an instance of the telegram bot */
7
8
  bot: TelegramBot;
9
+ /** an instance of the telegram update */
8
10
  update: TelegramUpdate;
11
+ /** string representing the type of update that was sent */
9
12
  update_type: string;
13
+ /** reference to TelegramApi class */
10
14
  api: TelegramApi;
11
- private data;
15
+ /**
16
+ * Create a telegram execution context
17
+ * @param bot - the telegram bot
18
+ * @param update - the telegram update
19
+ */
12
20
  constructor(bot: TelegramBot, update: TelegramUpdate);
13
- getText(): string;
14
- next(): Response;
15
- setData(key: string, value: SerializableData): this;
16
- getData(key: string): SerializableData;
17
- replyVideo(video: string, options?: Record<string, SerializableData>): Promise<Response | undefined>;
21
+ /**
22
+ * Reply to the last message with a video
23
+ * @param video - string to a video on the internet or a file_id on telegram
24
+ * @param options - any additional options to pass to sendVideo
25
+ */
26
+ replyVideo(video: string, options?: Record<string, number | string | boolean>): Promise<Response | undefined>;
27
+ /**
28
+ * Get File from telegram file_id
29
+ * @param file_id - telegram file_id
30
+ */
18
31
  getFile(file_id: string): Promise<Response>;
19
- replyPhoto(photo: string, caption?: string, options?: Record<string, SerializableData>): Promise<Response | undefined>;
20
- reply(message: string, parse_mode?: string, options?: Record<string, SerializableData>): Promise<Response | undefined>;
32
+ /**
33
+ * Reply to the last message with a photo
34
+ * @param photo - url or file_id to photo
35
+ * @param caption - photo caption
36
+ * @param options - any additional options to pass to sendPhoto
37
+ */
38
+ replyPhoto(photo: string, caption?: string, options?: Record<string, number | string | boolean>): Promise<Response | undefined>;
39
+ /**
40
+ * Reply to the last message with text
41
+ * @param message - text to reply with
42
+ * @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
43
+ * @param options - any additional options to pass to sendMessage
44
+ */
45
+ reply(message: string, parse_mode?: string, options?: Record<string, number | string | boolean>): Promise<Response | undefined>;
21
46
  }
@@ -2,12 +2,21 @@ import TelegramApi from './telegram_api.js';
2
2
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
3
3
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
4
4
  import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
5
+ /** Class representing the context of execution */
5
6
  export default class TelegramExecutionContext {
7
+ /** an instance of the telegram bot */
6
8
  bot;
9
+ /** an instance of the telegram update */
7
10
  update;
11
+ /** string representing the type of update that was sent */
8
12
  update_type = '';
13
+ /** reference to TelegramApi class */
9
14
  api = new TelegramApi();
10
- data = {};
15
+ /**
16
+ * Create a telegram execution context
17
+ * @param bot - the telegram bot
18
+ * @param update - the telegram update
19
+ */
11
20
  constructor(bot, update) {
12
21
  this.bot = bot;
13
22
  this.update = update;
@@ -24,19 +33,11 @@ export default class TelegramExecutionContext {
24
33
  this.update_type = 'document';
25
34
  }
26
35
  }
27
- getText() {
28
- return this.update.message?.text ?? this.update.inline_query?.query ?? '';
29
- }
30
- next() {
31
- return new Response('ok');
32
- }
33
- setData(key, value) {
34
- this.data[key] = value;
35
- return this;
36
- }
37
- getData(key) {
38
- return this.data[key];
39
- }
36
+ /**
37
+ * Reply to the last message with a video
38
+ * @param video - string to a video on the internet or a file_id on telegram
39
+ * @param options - any additional options to pass to sendVideo
40
+ */
40
41
  async replyVideo(video, options = {}) {
41
42
  switch (this.update_type) {
42
43
  case 'message':
@@ -56,9 +57,19 @@ export default class TelegramExecutionContext {
56
57
  break;
57
58
  }
58
59
  }
60
+ /**
61
+ * Get File from telegram file_id
62
+ * @param file_id - telegram file_id
63
+ */
59
64
  async getFile(file_id) {
60
65
  return await this.api.getFile(this.bot.api.toString(), { file_id }, this.bot.token);
61
66
  }
67
+ /**
68
+ * Reply to the last message with a photo
69
+ * @param photo - url or file_id to photo
70
+ * @param caption - photo caption
71
+ * @param options - any additional options to pass to sendPhoto
72
+ */
62
73
  async replyPhoto(photo, caption = '', options = {}) {
63
74
  switch (this.update_type) {
64
75
  case 'photo':
@@ -86,6 +97,12 @@ export default class TelegramExecutionContext {
86
97
  break;
87
98
  }
88
99
  }
100
+ /**
101
+ * Reply to the last message with text
102
+ * @param message - text to reply with
103
+ * @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
104
+ * @param options - any additional options to pass to sendMessage
105
+ */
89
106
  async reply(message, parse_mode = '', options = {}) {
90
107
  switch (this.update_type) {
91
108
  case 'message':
package/dist/types.d.ts CHANGED
@@ -3,12 +3,7 @@ import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
5
  import TelegramCommand from './types/TelegramCommand.js';
6
- import Kv from './types/Kv.js';
7
- import localhost from './types/localhost.js';
8
6
  import WebhookCommands from './types/WebhookCommands.js';
9
- import Joke from './types/Joke.js';
10
- import Bored from './types/Bored.js';
11
- import Balance from './types/Balance.js';
12
7
  import TelegramFrom from './types/TelegramFrom.js';
13
8
  import TelegramChat from './types/TelegramChat.js';
14
9
  import TelegramUser from './types/TelegramUser.js';
@@ -24,9 +19,6 @@ import TelegramInlineQueryType from './types/TelegramInlineQueryType.js';
24
19
  import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
25
20
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
26
21
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
27
- import DDGQueryResponse from './types/DDGQueryResponse.js';
28
22
  import ChatPermissions from './types/ChatPermissions.js';
29
- import Kanye from './types/Kanye.js';
30
- import SerializableData from './types/SerializableData.js';
31
23
  export default TelegramBot;
32
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, 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, };
24
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramCommand, WebhookCommands, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, ChatPermissions, };
package/dist/types.js CHANGED
@@ -2,7 +2,6 @@ import TelegramBot from './telegram_bot.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
- import localhost from './types/localhost.js';
6
5
  import WebhookCommands from './types/WebhookCommands.js';
7
6
  import Update from './types/Update.js';
8
7
  import TelegramUpdate from './types/TelegramUpdate.js';
@@ -10,4 +9,4 @@ import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
10
9
  import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
11
10
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
12
11
  export default TelegramBot;
13
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, localhost, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
12
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "7.20.0",
3
+ "version": "7.23.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",
package/dist/api.d.ts DELETED
@@ -1,30 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import { SerializableData, TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto } from './types';
3
- import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo';
4
- export default class API {
5
- static getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): Request<unknown, CfProperties<unknown>>;
6
- static getFile(botApi: string, data: {
7
- file_id: string;
8
- }, token: string): Promise<ArrayBuffer>;
9
- static sendMessage(botApi: string, data: {
10
- reply_to_message_id: number | string;
11
- chat_id: number | string;
12
- text: string;
13
- parse_mode: string;
14
- }): Promise<Response>;
15
- static sendVideo(botApi: string, data: {
16
- reply_to_message_id: number | string;
17
- chat_id: number | string;
18
- video: string;
19
- }): Promise<Response>;
20
- static sendPhoto(botApi: string, data: {
21
- reply_to_message_id: number | string;
22
- chat_id: number | string;
23
- photo: string;
24
- caption: string;
25
- }): Promise<Response>;
26
- static answerInline(botApi: string, data: {
27
- inline_query_id: number | string;
28
- results: TelegramInlineQueryResultArticle[] | TelegramInlineQueryResultPhoto[] | TelegramInlineQueryResultVideo[];
29
- }): Promise<Response>;
30
- }
package/dist/api.js DELETED
@@ -1,37 +0,0 @@
1
- export default class API {
2
- static getApiUrl(botApi, slug, data) {
3
- const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
4
- const params = new URLSearchParams();
5
- for (const i in data) {
6
- params.append(i, data[i].toString());
7
- }
8
- return new Request(`${request}?${params}`);
9
- }
10
- static async getFile(botApi, data, token) {
11
- const url = this.getApiUrl(botApi, 'getFile', data);
12
- const response = await fetch(url);
13
- const json = (await response.json());
14
- const file_path = json.result.file_path;
15
- const file_response = await fetch(`https://api.telegram.org/file/bot${token}/${file_path}`);
16
- return await file_response.arrayBuffer();
17
- }
18
- static async sendMessage(botApi, data) {
19
- const url = this.getApiUrl(botApi, 'sendMessage', data);
20
- return await fetch(url);
21
- }
22
- static async sendVideo(botApi, data) {
23
- const url = this.getApiUrl(botApi, 'sendVideo', data);
24
- return await fetch(url);
25
- }
26
- static async sendPhoto(botApi, data) {
27
- const url = this.getApiUrl(botApi, 'sendPhoto', data);
28
- return await fetch(url);
29
- }
30
- static async answerInline(botApi, data) {
31
- const url = this.getApiUrl(botApi, 'answerInlineQuery', {
32
- inline_query_id: data.inline_query_id,
33
- results: JSON.stringify(data.results),
34
- });
35
- return await fetch(url);
36
- }
37
- }
package/dist/ctx.d.ts DELETED
@@ -1,19 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import TelegramBot from './telegram_bot';
3
- import { SerializableData, TelegramUpdate } from './types';
4
- export default class TelegramExecutionContext {
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
- replyVideo(video: string): Promise<Response | undefined>;
16
- getFile(file_id: string): Promise<ArrayBuffer>;
17
- replyPhoto(photo: string, caption?: string): Promise<Response | undefined>;
18
- reply(message: string, parse_mode?: string): Promise<Response | undefined>;
19
- }
package/dist/ctx.js DELETED
@@ -1,109 +0,0 @@
1
- import API from './api';
2
- import { TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto } from './types';
3
- import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo';
4
- export default class TelegramExecutionContext {
5
- bot;
6
- update;
7
- update_type = '';
8
- data = {};
9
- constructor(bot, update) {
10
- this.bot = bot;
11
- this.update = update;
12
- if (this.update.message?.photo) {
13
- this.update_type = 'photo';
14
- }
15
- else if (this.update.message?.text) {
16
- this.update_type = 'message';
17
- }
18
- else if (this.update.inline_query?.query) {
19
- this.update_type = 'inline';
20
- }
21
- }
22
- getText() {
23
- return this.update.message?.text || this.update.inline_query?.query || '';
24
- }
25
- next() {
26
- return new Response('ok');
27
- }
28
- setData(key, value) {
29
- this.data[key] = value;
30
- return this;
31
- }
32
- deleteData(key) {
33
- delete this.data[key];
34
- return this;
35
- }
36
- getData(key) {
37
- return this.data[key];
38
- }
39
- async replyVideo(video) {
40
- switch (this.update_type) {
41
- case 'message':
42
- return await API.sendVideo(this.bot.api.toString(), {
43
- chat_id: this.update.message?.chat.id.toString() ?? '',
44
- reply_to_message_id: this.update.message?.message_id.toString() ?? '',
45
- video,
46
- });
47
- case 'inline':
48
- return await API.answerInline(this.bot.api.toString(), {
49
- inline_query_id: this.update.inline_query?.id.toString() ?? '',
50
- results: [new TelegramInlineQueryResultVideo(video)],
51
- });
52
- default:
53
- break;
54
- }
55
- }
56
- async getFile(file_id) {
57
- return await API.getFile(this.bot.api.toString(), { file_id }, this.bot.token);
58
- }
59
- async replyPhoto(photo, caption = '') {
60
- switch (this.update_type) {
61
- case 'photo':
62
- return await API.sendPhoto(this.bot.api.toString(), {
63
- chat_id: this.update.message?.chat.id.toString() ?? '',
64
- reply_to_message_id: this.update.message?.message_id.toString() ?? '',
65
- photo,
66
- caption,
67
- });
68
- case 'message':
69
- return await API.sendPhoto(this.bot.api.toString(), {
70
- chat_id: this.update.message?.chat.id.toString() ?? '',
71
- reply_to_message_id: this.update.message?.message_id.toString() ?? '',
72
- photo,
73
- caption,
74
- });
75
- case 'inline':
76
- return await API.answerInline(this.bot.api.toString(), {
77
- inline_query_id: this.update.inline_query?.id.toString() ?? '',
78
- results: [new TelegramInlineQueryResultPhoto(photo)],
79
- });
80
- default:
81
- break;
82
- }
83
- }
84
- async reply(message, parse_mode = '') {
85
- switch (this.update_type) {
86
- case 'message':
87
- return await API.sendMessage(this.bot.api.toString(), {
88
- chat_id: this.update.message?.chat.id.toString() ?? '',
89
- reply_to_message_id: this.update.message?.message_id.toString() ?? '',
90
- text: message,
91
- parse_mode,
92
- });
93
- case 'photo':
94
- return await API.sendMessage(this.bot.api.toString(), {
95
- chat_id: this.update.message?.chat.id.toString() ?? '',
96
- reply_to_message_id: this.update.message?.message_id.toString() ?? '',
97
- text: message,
98
- parse_mode,
99
- });
100
- case 'inline':
101
- return await API.answerInline(this.bot.api.toString(), {
102
- inline_query_id: this.update.inline_query?.id.toString() ?? '',
103
- results: [new TelegramInlineQueryResultArticle(message)],
104
- });
105
- default:
106
- break;
107
- }
108
- }
109
- }
File without changes
@@ -1 +0,0 @@
1
- "use strict";
File without changes
@@ -1 +0,0 @@
1
- "use strict";