Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 init #2

Merged
Merged

Conversation

Keyrxng
Copy link
Collaborator

@Keyrxng Keyrxng commented Aug 30, 2024

Resolves ubiquity-os/plugins-wishlist#25
Required by #1

Dual action/worker plugin serving as Telegram Bot kernel and an UbiquityOS plugin.

Requirements

Config:

  - uses:
    - plugin: ubiquity-os-marketplace/telegram-bridge@development
      with:
        botId: 7487982261
  - uses:  
    - plugin: http://localhost:3000
      with:
        botId: 7487982261
  • Database: Run the migration inside the Supabase SQL editor. Obtain url and service key.
  • Repository: Make yourself an admin of the repo. Mint a classic PAT, repo scoped.
  • TG Bot: Contact the BotFather and create a new bot, obtain the token for it.
  • TG Account: Head to my.telegram and obtain the api_hash and app_id.
  • Smee: Create a new url at https://smee.io/new.
  • yarn setup-env: It'll run through each set of env vars, store and upload them. Enter 0 for BOT_ADMINS for now, don't wrap input in quotes and if you need to run it more than once and you are copy pasting from .dev.vars you should sanitize the additional \ applied to line breaks in the private key.
  • yarn sms-auth: It'll ask for details via the terminal, store the session and exit.
  • TG Webhook: smee -u <url> -P "/webhook" in terminal B
  • yarn worker: should be working at this point. Speak to your bot on TG and use /setcommands.

If you have any issues at this stage let me know please.

To use the workflow functions simply create a new task in a repo and assign a price label. Check the actions tab of the telegram-bot repo and wait. Close, repeat. Reopen, repeat.

Everything should be running smoothly. I E2E tested with new DBs etc a couple of times so hopefully there is no issues for anyone testing.


Features

Commands

  • /setcommands - private, admin
  • /chatid - private, groups
  • /botid - private
  • /myid - private

Workflow functions

  • create-chat: Creates a workroom per task once price labeled. @org/repo#1 url schema and posts an invite link on the task
  • close-chat: Kicks everyone once the task is closed, archives the chat and snapshots userIds.
  • reopen-chat: Unarchives the chat, re-invites everyone previously involved.

Environment helpers

  • setup-env: Uses the CLI to walk through saving env vars locally and pushing them to repo secrets.
  • sms-auth: Requires the personal account holder to create a session via 2FA confirmed via a Telegram client, i.e you'll get a desktop notification from TG with the code.

Copy link
Contributor

github-actions bot commented Aug 30, 2024

Unused devDependencies (2)

Filename devDependencies
package.json smee-client
supabase

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Aug 30, 2024

rfc @0x4007 @gentlementlegen @rndquu @whilefoo

I'll clean up and streamline things a lot if I should continue ahead with this approach

Copy link
Contributor

@whilefoo whilefoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what's going in the code, I'm guessing it's a lot of boilerplate. But the full picture I got is that when issue is created the kernel sends event to this plugin which creates a workroom and deletes it when the issue is deleted.
I also saw there is a telegram webhook, what is the purpose of that?

@0x4007
Copy link
Member

0x4007 commented Sep 2, 2024

Let's do individual chat rooms instead of a group with topics. This is to maximize signal to noise.

Also the data should not be deleted, instead, the users should be kicked when the project is closed as complete. This is to reduce clutter on one's telegram inbox and search history. If necessary they can always rejoin from the original link.

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 2, 2024

@whilefoo sorry mate I had some commits I hadn't pushed but to sum things up so far:

This worker is split into two distinct parts.

A: Hono app which supports the telegram bot
B: Standard Ubiquity-OS plugin architecture

A - We are hooked into our BotFather bot and this is the "kernel" for it. So we'll create all commands, functions etc through this worker which is separate from but able to interact with the plugin side.

B - We are also hooked into the kernel as a plugin and in this PR you see the callbacks relating to the "workrooms" feature only.


I'm guessing it's a lot of boilerplate

Yeah still a little bit of boilerplate and a few helper commands I've added for setup idk if they'll be kept. I expected that if this PR progressed I'd need to build in basic commands and features so I haven't cleaned up completely.

But the full picture I got is that when issue is created the kernel sends event to this plugin which creates a workroom and deletes it when the issue is deleted

  1. issues.labeled fires and kernel sends payload to this worker
  2. Github path uses the tg bot api to create/close workrooms, post github comment etc
  3. Bot path can hear the event for new chat created and we can act on forwarding that message etc.
  4. We do not delete chats but we do update the DB with a "closed" status and close/kick everyone from it.
  5. Still to add some safety checks like is it a price label that's been added etc.

This feature aside, it'll house all telegram bot chat commands and features etc.


The task discussion lays out a spec to enable the opening of new group chats from a personal account vs this implementation which is using a main supergroup and opening forums (topics). I spent hours trying various libs and approaches and decided to progress everything else and either it can be tasked out or if it's a blocker more time spent on it.

Here is the troubles with that so far:

  1. The endpoint is not REST and requires special auth handling. There are libraries and frameworks built for the MTProto API however, because we are building Cloudflare workers and not hosting a node server for example packages do not work out of the box and/or they are just incompatible with that env.
  2. Frameworks and libs are built targeting browser or node, while cloudlfare is a weird custom mix of both for using modules such as crypto which is essential in these libs for handling auth and sessions which would need patched for deployment.
  3. We could attempt to handle all that these frameworks do re: MTProto auth, sessions etc and build with Cloudflare V8 in mind but I'm not sure that's worth the time investment.
  4. Implementation details aside, it's not ergonomic for a number of reasons to use separate group chats for each new workroom. The idea behind them is empowering collaboration and oversight of off-site task related activity.
  • If they are separate chats we have no global overview.
  • 3rd parties like investors eg cannot see activity in these without joining them directly.
  • They'll only have the team member(s) that join the workroom when it's created unless we automate that

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 2, 2024

Let's do individual chat rooms instead of a group with topics. This is to maximize signal to noise.

Also the data should not be deleted, instead, the users should be kicked when the project is closed as complete. This is to reduce clutter on one's telegram inbox and search history. If necessary they can always rejoin from the original link.

Only saw this after posting my comment above.

Agreed, no chats are deleted. I mentioned my troubles with the MTProto API so far and tried all that I could think over the weekend but had very little luck and it involved a lot of "hacks" like patching node_modules because cloudflare wants any Node module prefixed with "node:" for a start, then the methods differ etc.

Anyway, I'll spend more time on it if need be but in regards to everything else, the way it communicates with the kernel and the way I have built callbacks and what that means for adding additional "plugin"-like features that respond to events, happy for me to proceed with this approach?

@0x4007
Copy link
Member

0x4007 commented Sep 2, 2024

Why not make some sort of GitHub actions based micro service that just opens chat rooms?

It sounds to me that cloudflare is limiting this due to incompatible node modules.

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 2, 2024

Why not make some sort of GitHub actions based micro service that just opens chat rooms?

It sounds to me that cloudflare is limiting this due to incompatible node modules.

I had considered that we'd likely use the workflows and the worker in unison depending on what sort of features we add but hadn't considered it for this feature but I will attempt to do that although it probably isn't pretty.

  1. issues.labeled fires, kernel forwards to this worker.
  2. worker needs to repository_dispatch to kick off the workflow (I think that's the best way)
  3. this worker cannot wait until the response so it would require a dedicated workflow and ts script
  4. The script may need packages separate from src although hopefully not.
  5. I struggle to think rn how to facilitate this worker communicating with itself elegantly through workflows and passing data between.

I'll see how I get on with it and report back

@whilefoo
Copy link
Contributor

whilefoo commented Sep 3, 2024

2. worker needs to repository_dispatch to kick off the workflow (I think that's the best way)

Why does the worker need to start the workflow, the kernel can directly start the workflow? Actually is there any reason why even use a worker if we move everything to the workflow?

@0x4007
Copy link
Member

0x4007 commented Sep 3, 2024

  1. worker needs to repository_dispatch to kick off the workflow (I think that's the best way)

Why does the worker need to start the workflow, the kernel can directly start the workflow? Actually is there any reason why even use a worker if we move everything to the workflow?

Eventually the bot should reply to messages on Telegram, so building for speed is important.

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 3, 2024

Actually is there any reason why even use a worker if we move everything to the workflow?

The workflow is a tool that the worker will use, it is not replacing the worker instance. So it would be a workflow which handles one action: create a group chat using MTProto API. The cf worker can dispatch to it's own repo to execute functions using a different env than what cf provides, which if it works could be awesome.

Why does the worker need to start the workflow, the kernel can directly start the workflow?

So this worker would repo_dispatch to it's own repo to execute the createGroupchat.yml workflow. This way it is not either-or for worker or workflow we can have both for this bot. I don't think it's possible to tell the kernel to fire off an arbitrary workflow is it as we have one entry which is compute.yml?

Eventually the bot should reply to messages on Telegram, so building for speed is important.

This is how I thought of things. If this plugin is a worker we have instant response on the telegram and github side instead of having a dedicated worker just handling telegram requests and us building plugins/features as plugins which interact with the telegram bot. While also having the benefit of dispatching workflows to it's own repo although I thought that would be much further down the line lol

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 30, 2024

Adding it solved my issue, I am successfully logged in.

I don't understand how they two items are related and why it prevented you from logging in when it never stopped me before.

I will keep as a migration and properly name it.

I would also advise to add yarn add -DE smee-client to have smee up and ready for the project, and update wrangler to the latest version to avoid warnings.

Will do.


Glad to hear you are now set up.

@gentlementlegen
Copy link
Member

It stopped me from logging in because the migration for init never ran so the table didn't exist so adding the entry into Supabase failed when running the script.

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 30, 2024

It stopped me from logging in because the migration for init never ran so the table didn't exist so adding the entry into Supabase failed when running the script.

Ah okay so it skipped it because you ran SB locally my bad, I assumed you'd use the UI my mistake.


With the Supabase CLI I get a successful run of supabase start + supabase db start. If I run db reset which should do the same as db start afaik but resets the local instance, I get errored out regarding container health.

Since I do not ever use Supabase this way, what do you expect to see in regards to how the local SB stuff should work?

The default config.toml comes with a host of unnecessary items but I'm not 100% which affect the local instances so is the default preferred similar to the default tsconfig.json. Remove only things that are commented out or everything that's not required i.e auth email etc...?

@gentlementlegen
Copy link
Member

I guess you are using windows if you get an error with the healthcheck. You have to opt out from the analytics in the toml and everything will be running.

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 30, 2024

I guess you are using windows if you get an error with the healthcheck. You have to opt out from the analytics in the toml and everything will be running.

much obliged fellow hipster

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Sep 30, 2024

The error remained but I gathered it's OS or my sys related so I moved on. I was able to setup a local DB so it appears to be working

@gentlementlegen
Copy link
Member

gentlementlegen commented Oct 1, 2024

@Keyrxng I think I have the full setup, I changed my configuration file and all that the kernel does when I try to use the command /myid is No handler found for event issue_comment. What am I doing wrong?

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Oct 1, 2024

@Keyrxng I think I have the full setup, I changed my configuration file and all that the kernel does when I try to use the command /myid is No handler found for event issue_comment. What am I doing wrong?

That's a telegram bot command, send that to your bot father bot on tg.

Only issues.labelled | closed | reopen will trigger a workflow function. The worker should respond to any bot command in the readme, you may have to use /setcommands on the tg bot too

I am not sure re: payload logging I will look into it first thing

README.md Outdated Show resolved Hide resolved
@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Oct 2, 2024

Should I make a task and create a setup tutorial, showing e2e functionality with videos, would that be beneficial?

Should I make a task and create a test suite that covers GitHub and both TG apis and start to work on that?

@Keyrxng Keyrxng mentioned this pull request Oct 3, 2024
@gentlementlegen
Copy link
Member

I think having the full list of steps numbered that ensure a successful setup would be enough. Sorry I'll test again the setup, working on multiple things.

@gentlementlegen
Copy link
Member

I confirm it works. I got a room chat created on issue creation:
image
Meniole#4

@gentlementlegen
Copy link
Member

@Keyrxng Could you add a migration script and type generation script similar to here?

@Keyrxng
Copy link
Collaborator Author

Keyrxng commented Oct 4, 2024

@Keyrxng Could you add a migration script and type generation script similar to here?

@gentlementlegen #3 removes the Supabase dependency completely and will be merged very soon, considering this should I still add your request?

@gentlementlegen
Copy link
Member

@Keyrxng then that is fine.

Copy link
Member

@gentlementlegen gentlementlegen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to add the manifest action later on for this to have the configuration checked by the kernel.

@whilefoo
Copy link
Contributor

whilefoo commented Oct 7, 2024

@Keyrxng I get this error when I put a label on an issue:

[2024-10-07T20:27:25.477] [INFO] - [Running gramJS version 2.24.10]
[2024-10-0[7](https://github.com/ubiquity-whilefoo/telegram-bot/actions/runs/11223403676/job/31197960956#step:6:8)T20:27:25.480] [INFO] - [Connecting to 149.154.167.91:80/TCPFull...]
[2024-10-07T20:27:25.5[8](https://github.com/ubiquity-whilefoo/telegram-bot/actions/runs/11223403676/job/31197960956#step:6:9)3] [INFO] - [Connection to 149.154.167.91:80/TCPFull complete!]
[2024-10-07T20:27:25.584] [INFO] - [Using LAYER 184 for initial connect]
	 Creating chat with name: 
	 {
	    "chatName": "@ubiquibot-whilefoo-testing/testing#1",
	    "caller": "createChat"
	  }
	     <anonymous> (/home/runner/work/telegram-bot/telegram-bot/src/handlers/workflow-proxy.ts:28:[9](https://github.com/ubiquity-whilefoo/telegram-bot/actions/runs/11223403676/job/31197960956#step:6:10))
	 Error in creating chat: 
	 {
	    "er": {},
	    "caller": "createChat"
	  }
	     <anonymous> (/home/runner/work/telegram-bot/telegram-bot/src/handlers/workflow-proxy.ts:28:9)

https://github.com/ubiquity-whilefoo/telegram-bot/actions/runs/11223403676/job/31197960956

@ubiquity-os ubiquity-os bot merged commit 77a12d5 into ubiquity-os-marketplace:development Oct 12, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Telegram GitHub Issue "Workroom"
5 participants