Skip to content

Commit

Permalink
Merge pull request #3 from ubq-testing/gh-storage
Browse files Browse the repository at this point in the history
DM Notifications
  • Loading branch information
Keyrxng authored Oct 28, 2024
2 parents 0825881 + 638537f commit ac855de
Show file tree
Hide file tree
Showing 65 changed files with 2,714 additions and 905 deletions.
13 changes: 9 additions & 4 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
{
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
"version": "0.2",
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "./src/adapters/supabase/**/**.ts", "supabase/config.toml"],
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log", "src/adapters/github/storage-layer.ts"],
"useGitignore": true,
"language": "en",
"words": [
"Withsha",
"setwebhook",
"Nektos",
"dataurl",
"devpool",
"outdir",
"servedir",
"Supabase",
"SUPABASE",
"typebox",
"ubiquibot",
"Smee",
Expand All @@ -35,7 +34,13 @@
"unarchived",
"libsodium",
"binkey",
"binsec"
"binsec",
"supabase",
"Supabase",
"userbase",
"Superbase",
"SUPABASE",
"CODEOWNER"
],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
Expand Down
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Only `yarn setup-env` requires these variables to be set
TELEGRAM_BOT_REPOSITORY_FULL_NAME=
GITHUB_PAT_TOKEN=
REPO_ADMIN_ACCESS_TOKEN=
2 changes: 1 addition & 1 deletion .github/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config: KnipConfig = {
ignore: ["src/types/config.ts", "**/__mocks__/**", "**/__fixtures__/**"],
ignoreExportsUsedInFile: true,
// eslint can also be safely ignored as per the docs: https://knip.dev/guides/handling-issues#eslint--jest
ignoreDependencies: ["eslint-config-prettier", "eslint-plugin-prettier", "@mswjs/data", "smee-client", "supabase"],
ignoreDependencies: ["eslint-config-prettier", "eslint-plugin-prettier", "@mswjs/data", "smee-client"],
eslint: true,
};

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/compute.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
permissions: write-all
env:
TELEGRAM_BOT_ENV: ${{ secrets.TELEGRAM_BOT_ENV }}
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}

steps:
- uses: actions/checkout@v4
Expand All @@ -45,3 +47,5 @@ jobs:
id: Kernel-Telegram
env:
TELEGRAM_BOT_ENV: ${{ secrets.TELEGRAM_BOT_ENV }}
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
4 changes: 4 additions & 0 deletions .github/workflows/worker-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ jobs:
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
secrets: |
TELEGRAM_BOT_ENV
APP_ID
APP_PRIVATE_KEY
env:
TELEGRAM_BOT_ENV: ${{ secrets.TELEGRAM_BOT_ENV }}
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}

- name: Write Deployment URL to Summary
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cypress/screenshots
script.ts
.wrangler
test-dashboard.md
t.ts

# prod
dist/
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ A Telegram bridge for Ubiquity OS, uniquely combining Cloudflare Workers and Git
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Environment Variables](#environment-variables)
- [Supabase Configuration](#supabase-configuration)
- [Telegram Configuration](#telegram-configuration)
- [GitHub Configuration](#github-configuration)
- [Usage](#usage)
Expand Down Expand Up @@ -65,13 +64,15 @@ This bot operates in two parts:

#### Environment Variables

The `TELEGRAM_BOT_ENV` is a single JSON object that encapsulates all necessary environment variables for the bot's operation. It consists of three key sections: `botSettings`, `mtProtoSettings`, and `storageSettings`.
The `TELEGRAM_BOT_ENV` is a single JSON object that encapsulates all necessary environment variables for the bot's operation. It consists of two key sections: `botSettings` and `mtProtoSettings`.

We also need `REPO_ADMIN_ACCESS_TOKEN` for setting up GitHub secrets during the initial setup. This token is used to save the environment variables as secrets in the repository settings.

You can set up your environment variables by using the provided utility script:

- Run `yarn setup-env`, which prompts you to enter each value via the CLI. The values will be serialized and stored both locally and in your repository secrets.

- **GITHUB_PAT_TOKEN**: Create a classic personal access token (PAT) with the `repo` scope. Set the expiry to 24 hours. This token will be used to generate repository secrets for the environment variables and will be removed from `.env` after the secrets are saved.
- **REPO_ADMIN_ACCESS_TOKEN**: Create a classic personal access token (PAT) with the `repo` scope. Set the expiry to 24 hours. This token will be used to generate repository secrets for the environment variables and will be removed from `.env` after the secrets are saved.
- **Account Permission**: The account in which the PAT is associated with _must_ be an `admin` of the repository to be able to save secrets this way. Visit your repository settings `telegram-bridge` > `Collaborators & teams` to add the account as an admin first if needed.

The environment variables are stored in the following locations:
Expand All @@ -84,7 +85,7 @@ The environment variables are stored in the following locations:

1. **botSettings**: Contains bot-specific settings like `TELEGRAM_BOT_TOKEN`, `TELEGRAM_BOT_WEBHOOK_SECRET`, etc.
2. **mtProtoSettings**: Contains settings for the MTProto API like `TELEGRAM_APP_ID`, `TELEGRAM_API_HASH`, etc.
3. **storageSettings**: Contains settings for the Supabase database like `SUPABASE_URL`, `SUPABASE_SERVICE_KEY`, etc.
3. **REPO_ADMIN_ACCESS_TOKEN**: GitHub Personal Access Token for saving secrets during setup, and used for TG bot commands.

```typescript
interface TELEGRAM_BOT_ENV {
Expand All @@ -98,10 +99,6 @@ interface TELEGRAM_BOT_ENV {
TELEGRAM_APP_ID: number; // Telegram App ID
TELEGRAM_API_HASH: string; // Telegram API Hash
};
storageSettings: {
SUPABASE_URL: string; // Supabase URL
SUPABASE_SERVICE_KEY: string; // Supabase Service Key
};
}
```

Expand Down Expand Up @@ -161,39 +158,43 @@ For more detailed information, refer to the official [Supabase documentation](ht

## Testing Locally

1. Spin up a Supabase instance and run the migration/copypaste the SQL file into the SQL editor.
2. Run `yarn setup-env` to set up your environment variables. `WEBHOOK_URL` should be set to your local Smee URL initially with no path.
3. Run `yarn sms-auth` to authenticate your personal Telegram account with MTProto. This will store your session in Supabase.
4. Run `yarn worker` to start the Cloudflare Worker instance.
5. Run `smee -u https://smee.io/your-webhook-url -P "/telegram"` to receive Telegram webhook payloads locally.
6. Define the plugin twice in your `ubiquibot-config.yml` file, one pointing at the Cloudflare Worker instance or localhost and the other at the GitHub Actions workflow.
7. Interact with the bot in Telegram chats or trigger GitHub webhooks as defined in `manifest.json`.
8. Run `yarn deploy` to deploy the Cloudflare Worker instance.
9. Paste or push your CF secrets but this time replace the `WEBHOOK_URL` with the Cloudflare Worker URL.
10. Once deployed, use `/setwebhook` to set the bot's webhook URL to the Cloudflare Worker instance. It may take a minute or two to propagate.
11. If you need to revert back to your Smee URL, then simply ping your local worker and it will reset the webhook URL (example below).
1. Run `yarn setup-env` to set up your environment variables. `WEBHOOK_URL` should be set to your local Smee URL initially with no path.
2. Run `yarn sms-auth` to authenticate your personal Telegram account with MTProto. This will store your session in a private storage repo.
3. Run `yarn worker` to start the Cloudflare Worker instance.
4. Run `smee -u https://smee.io/your-webhook-url -P "/telegram"` to receive Telegram webhook payloads locally.
5. Define the plugin twice in your `ubiquibot-config.yml` file, one pointing at the Cloudflare Worker instance or localhost and the other at the GitHub Actions workflow.
6. Interact with the bot in Telegram chats or trigger GitHub webhooks as defined in `manifest.json`.
7. Run `yarn deploy` to deploy the Cloudflare Worker instance.
8. Paste or push your CF secrets but this time replace the `WEBHOOK_URL` with the Cloudflare Worker URL.
9. Once deployed, use `/setwebhook` to set the bot's webhook URL to the Cloudflare Worker instance. It may take a minute or two to propagate.
10. If you need to revert back to your Smee URL, then simply ping your local worker and it will reset the webhook URL (example below).

```bash
curl -X POST http://localhost:3000/telegram -H "Content-Type: application/json" -d '{"message": ""}'
```

- If you have to ping and reset the webhook URL, you will see an `unauthorized` error in the worker logs. This is expected and you can verify a successful reset by using a command like `/myid`.
- When setting your webhook url, it may take between 1-2 minutes to propagate, send one or two `/myid` commands and once it finally sets, the bot will respond asynchronously.

### Commands

- **/myid**: Get your Telegram User ID.
- **/botid**: Get the bot's Telegram User ID.
- **/chatid**: Get the chat ID.
- **/setwebhook**: Set the bot's webhook url.
- **/setcommands**: Set the bot's commands.
- **/register**: Pair your GitHub account with your Telegram account.
- **/subscribe**: Subscribe to various private notifications from the bot.
- **/unsubscribe**: Unsubscribe from private notifications.

## Repository Structure

```plaintext
.
├── .github/ # GitHub Actions workflows (CI/CD, not for workflow-function logic)
├── manifest.json # Plugin manifest for Ubiquity OS Kernel
├── supabase/ # SQL migration files for Supabase schema
├── src/ # Source code
│ ├── adapters/ # Storage adapters (e.g., Supabase integrations)
│ ├── adapters/ # Storage adapters (e.g., GitHub storage layer)
│ ├── bot/ # Core Telegram bot functionality (Worker and Workflow)
│ │ ├── features/ # Bot features, including commands and event handlers
│ │ ├── filters/ # Grammy filters (e.g., isAdmin, isPrivateChat)
Expand All @@ -216,4 +217,3 @@ curl -X POST http://localhost:3000/telegram -H "Content-Type: application/json"
## Considerations

- The `WEBHOOK_URL` is set on each call essentially, so the worker should always have it's own URL set as the webhook environment variable. Your local worker preferably retains the Smee URL env var which allows you to switch between the two easily.
- If you have to ping and reset the webhook URL, you will see an `unauthorized` error in the worker logs. This is expected and you can verify a successful reset by using a command like `/myid`.
12 changes: 10 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{
"name": "ubiquity-os/kernel-telegram",
"name": "ubq-testing/telegram--bot",
"description": "Part kernel plugin, part Telegram kernel. This bot is designed to be a bridge between UbiquityOS and Telegram.",
"ubiquity:listeners": ["issues.closed", "issues.reopened", "issues.assigned"]
"ubiquity:listeners": [
"issues.assigned",
"issues.closed",
"issues.reopened",
"issue_comment.created",
"issue_comment.edited",
"issues.unassigned",
"pull_request.review_requested"
]
}
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"prepare": "husky install",
"test": "jest --setupFiles dotenv/config --coverage",
"worker": "wrangler dev --env dev --port 3000",
"dev": "tsc-watch --onSuccess \"tsx ./src/main.ts\"",
"start": "tsx ./src/main.ts",
"deploy": "wrangler deploy --env dev",
"sms-auth": "npx tsx src/bot/mtproto-api/bot/scripts/sms-auth/sms-auth.ts",
"setup-env": "npx tsx src/bot/mtproto-api/bot/scripts/sms-auth/setup-env.ts"
Expand All @@ -42,13 +40,14 @@
"@octokit/rest": "20.1.1",
"@octokit/webhooks": "13.2.7",
"@sinclair/typebox": "0.32.33",
"@supabase/supabase-js": "^2.45.3",
"@supabase/supabase-js": "^2.45.6",
"@ubiquity-dao/ubiquibot-logger": "^1.3.1",
"big-integer": "^1.6.52",
"dotenv": "16.4.5",
"grammy": "^1.29.0",
"grammy-guard": "0.5.0",
"hono": "^4.5.9",
"octokit": "^4.0.2",
"telegram": "^2.24.11",
"typebox-validators": "0.3.5"
},
Expand Down Expand Up @@ -81,9 +80,7 @@
"npm-run-all": "4.1.5",
"prettier": "3.3.3",
"smee-client": "2.0.3",
"supabase": "^1.200.3",
"ts-jest": "29.2.5",
"tsc-watch": "^6.2.0",
"tsx": "4.18.0",
"typescript": "5.5.4",
"typescript-eslint": "8.3.0",
Expand Down
Loading

0 comments on commit ac855de

Please sign in to comment.