@codebam/cf-workers-telegram-bot 7.3.0 → 7.6.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,23 +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
8
-
9
- To get continuous conversation with AI working make sure you add a database
10
- to your wrangler.toml and initailize it with the schema.sql
11
-
12
24
  ```sh
13
25
  npm i @codebam/cf-workers-telegram-bot
14
26
  ```
15
27
 
16
- See `worker.ts` and follow the instructions below.
28
+ See `worker.ts` for an example of what a bot might look like. Just import from `@codebam/cf-workers-telegram-bot`.
29
+
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`
35
+
36
+ To set up GitHub actions to deploy when you push, see https://github.com/cloudflare/wrangler-action
17
37
 
18
38
  ---
19
39
 
20
- 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)
21
45
 
22
46
  - Click the deploy button
23
47
  - Navigate to your new **GitHub repository &gt; Settings &gt; Secrets** and add the following secrets:
@@ -31,19 +55,3 @@ To use the deploy button:
31
55
  ```
32
56
 
33
57
  - Push to `master` to trigger a deploy
34
-
35
- To fork this repo and use wrangler:
36
-
37
- - Click fork
38
- - `npm i -g wrangler`
39
- - `wrangler secret put SECRET_TELEGRAM_API_TOKEN` and set it to your telegram
40
- bot token
41
- - `wrangler d1 create llama2`
42
- - put the database block from the command in your wrangler.toml
43
- - `wrangler d1 execute --remote llama2 --file ./schema.sql`
44
- - `wrangler deploy`
45
- - Done!
46
-
47
- ## Getting started with cf-workers-telegram-bot
48
-
49
- Once you've deployed the bot you can get your Webhook command URL by calling `await bot.webhook.set()`
@@ -8,7 +8,7 @@ export default class TelegramBot {
8
8
  update_type: string;
9
9
  constructor(token: string);
10
10
  on(event: string, callback: () => Promise<Response>): this;
11
- handle(request: Request): Promise<any>;
11
+ handle(request: Request): Promise<Response>;
12
12
  reply(message: string): Promise<void>;
13
13
  }
14
14
  declare class Webhook {
@@ -13,9 +13,11 @@ export default class TelegramBot {
13
13
  this.update_type = '';
14
14
  }
15
15
  on(event, callback) {
16
- // eslint-disable-next-line
17
- // @ts-ignore TS7053
18
- this[event] = callback;
16
+ if (event !== 'on') {
17
+ // eslint-disable-next-line
18
+ // @ts-ignore TS7053
19
+ this[event] = callback;
20
+ }
19
21
  return this;
20
22
  }
21
23
  async handle(request) {
@@ -60,7 +62,7 @@ export default class TelegramBot {
60
62
  }
61
63
  // eslint-disable-next-line
62
64
  // @ts-ignore
63
- if (!this[command]) {
65
+ if (!this[command] || command === 'on') {
64
66
  command = 'default';
65
67
  }
66
68
  // eslint-disable-next-line
@@ -1,5 +1,5 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- interface Environment {
2
+ export interface Environment {
3
3
  SECRET_TELEGRAM_API_TOKEN: string;
4
4
  KV_GET_SET: KVNamespace;
5
5
  KV_UID_DATA: KVNamespace;
@@ -13,6 +13,6 @@ interface Environment {
13
13
  CHAT_MODEL: string;
14
14
  }
15
15
  declare const _default: {
16
- fetch: (request: Request, env: Environment) => Promise<Response>;
16
+ fetch: (request: Request, env: Environment, ctx: ExecutionContext) => Promise<Response>;
17
17
  };
18
18
  export default _default;
@@ -1,6 +1,7 @@
1
1
  import TelegramBot from '../../main/src/telegram_bot';
2
2
  export default {
3
- fetch: async (request, env) => {
3
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
4
+ fetch: async (request, env, ctx) => {
4
5
  const bot = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN);
5
6
  await bot
6
7
  .on('default', async function () {
@@ -62,11 +63,10 @@ export default {
62
63
  .on('default', async function () {
63
64
  switch (bot3.update_type) {
64
65
  case 'inline': {
65
- const { translated_text } = await env.AI.run('@cf/meta/m2m100-1.2b', {
66
- text: bot3.update.inline_query?.query.toString() ?? '',
67
- source_lang: 'french',
68
- target_lang: 'english',
69
- });
66
+ const translated_text = await fetch('https://clients5.google.com/translate_a/t?client=at&sl=auto&tl=en&q=' +
67
+ encodeURIComponent(bot3.update.inline_query?.query.toString() ?? ''))
68
+ .then((r) => r.json())
69
+ .then((json) => json[0].slice(0, -1).join(' '));
70
70
  await bot3.reply(translated_text ?? '');
71
71
  break;
72
72
  }
package/package.json CHANGED
@@ -1,45 +1,47 @@
1
1
  {
2
- "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "7.3.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.1.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": "55015e53958e69fc128e0b1e8116abb249291b0f"
2
+ "name": "@codebam/cf-workers-telegram-bot",
3
+ "version": "7.6.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": "d2c50965ef1d04fb62c9683f4418a7f4931ded93"
45
47
  }