mirror of
https://github.com/MichaByte/OnBoard-Live.git
synced 2025-12-06 07:13:41 -05:00
Initial commit
This commit is contained in:
commit
5ad56fac3f
21 changed files with 808 additions and 0 deletions
188
stream/backend/main.py
Normal file
188
stream/backend/main.py
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
from fastapi import FastAPI, Request, Response
|
||||
from prisma import Prisma
|
||||
from secrets import token_hex
|
||||
import asyncio
|
||||
from slack_bolt import Ack, App, Say
|
||||
from slack_bolt.adapter.fastapi import SlackRequestHandler
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
import requests
|
||||
|
||||
load_dotenv()
|
||||
|
||||
api = FastAPI()
|
||||
|
||||
db = Prisma()
|
||||
|
||||
bolt = App(
|
||||
token=os.environ["SLACK_TOKEN"], signing_secret=os.environ["SLACK_SIGNING_SECRET"]
|
||||
)
|
||||
|
||||
bolt_handler = SlackRequestHandler(bolt)
|
||||
|
||||
|
||||
@bolt.event("")
|
||||
@api.get("/api/v1/stream_key/{stream_key}")
|
||||
async def get_stream_by_key(stream_key: str):
|
||||
await db.connect()
|
||||
stream = await db.stream.find_first(where={"key": stream_key})
|
||||
await db.disconnect()
|
||||
return (
|
||||
stream if stream else Response(status_code=404, content="404: Stream not found")
|
||||
)
|
||||
|
||||
|
||||
@api.get("/api/v1/user/{user_id}")
|
||||
async def get_user_by_id(user_id: str):
|
||||
await db.connect()
|
||||
user = await db.user.find_first(where={"slack_id": user_id})
|
||||
await db.disconnect()
|
||||
return user if user else Response(status_code=404, content="404: User not found")
|
||||
|
||||
|
||||
@api.post("/api/v1/user")
|
||||
async def create_user(user: dict):
|
||||
await db.connect()
|
||||
try:
|
||||
new_user = await db.user.create(
|
||||
{"slack_id": user["slack_id"], "name": user["name"]}
|
||||
)
|
||||
print(new_user.id)
|
||||
new_stream = await db.stream.create(
|
||||
{"user": {"connect": {"id": new_user.id}}, "key": token_hex(16)}
|
||||
)
|
||||
print(new_user, new_stream)
|
||||
return new_user, new_stream
|
||||
except Exception as e:
|
||||
await db.disconnect()
|
||||
return Response(status_code=500, content=f"500: {str(e)}")
|
||||
finally:
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
@bolt.event("app_home_opened")
|
||||
def handle_app_home_opened_events(body, logger, event, client):
|
||||
client.views_publish(
|
||||
user_id=event["user"],
|
||||
# the view object that appears in the app home
|
||||
view={
|
||||
"type": "home",
|
||||
"callback_id": "home_view",
|
||||
# body of the view
|
||||
"blocks": [
|
||||
{
|
||||
"type": "section",
|
||||
"text": {
|
||||
"type": "mrkdwn",
|
||||
"text": "Welcome to OnBoard Live! Try sending `/onboard-live-apply` in the #onboard-live channel to get started!",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@bolt.view("apply")
|
||||
def handle_application_submission(ack, body):
|
||||
ack()
|
||||
print(body)
|
||||
convo = bolt.client.conversations_open(users=body["user"]["id"], return_im=True)
|
||||
bolt.client.chat_postMessage(
|
||||
channel=convo["channel"]["id"], text=f"Your application has been submitted! We will review it shortly. Please do not send another application - If you haven't heard back in over 48 hours, message @mra! Here's a copy of your responses for your reference:\n{body['view']['state']['values']['project-info']['project-desc-value']['value']}"
|
||||
)
|
||||
|
||||
|
||||
@bolt.command("/onboard-live-apply")
|
||||
def apply(ack: Ack, command):
|
||||
ack()
|
||||
print(command)
|
||||
r = requests.post(
|
||||
"https://slack.com/api/views.open",
|
||||
headers={"Authorization": f"Bearer {os.environ['SLACK_TOKEN']}"},
|
||||
json={
|
||||
"trigger_id": command["trigger_id"],
|
||||
"view": {
|
||||
"type": "modal",
|
||||
"callback_id": "apply",
|
||||
"title": {"type": "plain_text", "text": "Apply to OnBoard Live"},
|
||||
"submit": {"type": "plain_text", "text": "Submit!"},
|
||||
"blocks": [
|
||||
{
|
||||
"type": "input",
|
||||
"block_id": "project-info",
|
||||
"label": {
|
||||
"type": "plain_text",
|
||||
"text": "Some info on your project(s)",
|
||||
},
|
||||
"element": {
|
||||
"type": "plain_text_input",
|
||||
"multiline": True,
|
||||
"action_id": "project-desc-value",
|
||||
"placeholder": {
|
||||
"type": "plain_text",
|
||||
"text": "I'm going to make...",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
)
|
||||
print(r.status_code, r.text)
|
||||
# bolt.client.modal(channel=command['channel_id'], user=command['user_id'], text="Application form for OnBoard Live", blocks=[{
|
||||
|
||||
|
||||
# "type": "header",
|
||||
# "text": {
|
||||
# "type": "plain_text",
|
||||
# "text": "Welcome to OnBoard Live!",
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# "type": "section",
|
||||
# "text": {
|
||||
# "type": "mrkdwn",
|
||||
# "text": "Before you can get designing, we need a little bit of info from you. All fields are required!"
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# "type": "divider"
|
||||
# },
|
||||
# {
|
||||
# "type": "input",
|
||||
# "element": {
|
||||
# "type": "plain_text_input",
|
||||
# "multiline": True,
|
||||
# "action_id": "project_ideas_input-action",
|
||||
# "placeholder": {
|
||||
# "type": "plain_text",
|
||||
# "text": "I want to make a..."
|
||||
# }
|
||||
# },
|
||||
# "label": {
|
||||
# "type": "plain_text",
|
||||
# "text": "What do you plan to make with OnBoard Live?\nThis can be changed anytime!",
|
||||
# }
|
||||
# },
|
||||
# {
|
||||
# "type": "divider"
|
||||
# },
|
||||
# {
|
||||
# "type": "actions",
|
||||
# "elements": [
|
||||
# {
|
||||
# "type": "button",
|
||||
# "text": {
|
||||
# "type": "plain_text",
|
||||
# "text": "Apply!",
|
||||
# },
|
||||
# "value": "apply",
|
||||
# "style": "primary",
|
||||
# "action_id": "actionId-0"
|
||||
|
||||
# }]}])
|
||||
|
||||
|
||||
@api.post("/slack/events")
|
||||
async def slack_event_endpoint(req: Request):
|
||||
return await bolt_handler.handle(req)
|
||||
20
stream/backend/migrations/20240719191810_init/migration.sql
Normal file
20
stream/backend/migrations/20240719191810_init/migration.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "Stream" (
|
||||
"id" TEXT NOT NULL PRIMARY KEY,
|
||||
"key" TEXT NOT NULL,
|
||||
"active" BOOLEAN NOT NULL DEFAULT false,
|
||||
"focused" BOOLEAN NOT NULL DEFAULT false,
|
||||
CONSTRAINT "Stream_key_fkey" FOREIGN KEY ("key") REFERENCES "User" ("slackId") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"slackId" TEXT NOT NULL PRIMARY KEY,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Stream_key_key" ON "Stream"("key");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_slackId_key" ON "User"("slackId");
|
||||
3
stream/backend/migrations/migration_lock.toml
Normal file
3
stream/backend/migrations/migration_lock.toml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
||||
28
stream/backend/schema.prisma
Normal file
28
stream/backend/schema.prisma
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
generator client {
|
||||
provider = "prisma-client-py"
|
||||
interface = "asyncio"
|
||||
recursive_type_depth = 5
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:./dev.db"
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(cuid())
|
||||
created_at DateTime @default(now())
|
||||
slack_id String @unique
|
||||
name String
|
||||
stream Stream?
|
||||
}
|
||||
|
||||
model Stream {
|
||||
id String @id @default(cuid())
|
||||
created_at DateTime @default(now())
|
||||
is_live Boolean @default(false)
|
||||
is_focused Boolean @default(false)
|
||||
key String @unique @default(uuid())
|
||||
user User @relation(fields: [user_id], references: [id])
|
||||
user_id String @unique
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue