x1ongzhu 1 éve
szülő
commit
570074af5c
6 módosított fájl, 62 hozzáadás és 4 törlés
  1. 1 0
      package.json
  2. 6 2
      src/index.ts
  3. 24 0
      src/routes/rcs-route.ts
  4. 27 0
      src/services/rcs-service.ts
  5. 3 2
      tsconfig.json
  6. 1 0
      wrangler.toml

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
   "devDependencies": {
     "@cloudflare/workers-types": "^4.20240208.0",
     "@types/bcryptjs": "^2.4.6",
+    "@types/node": "^20.11.30",
     "wrangler": "^3.32.0"
   }
 }

+ 6 - 2
src/index.ts

@@ -1,12 +1,15 @@
 import { Context, Hono, Next } from "hono"
-import UserRoute from "./routes/users-route"
-import AuthRoute from "./routes/auth-route"
+
 import { cors } from "hono/cors"
 import { jwt, decode, sign, verify } from "hono/jwt"
 import { logger } from "hono/logger"
 import { showRoutes } from "hono/dev"
 import { HTTPException } from "hono/http-exception"
 
+import UserRoute from "./routes/users-route"
+import AuthRoute from "./routes/auth-route"
+import RcsRoute from "./routes/rcs-route"
+
 const app = new Hono<Environment>()
 
 const jwtMiddleware = (c: Context, next: Next) => {
@@ -21,6 +24,7 @@ app.use("/users/*", jwtMiddleware)
 
 app.route("/auth", AuthRoute)
 app.route("/users", UserRoute)
+app.route("/rcs", RcsRoute)
 
 app.onError(async (err, c) => {
     if (err instanceof HTTPException) {

+ 24 - 0
src/routes/rcs-route.ts

@@ -0,0 +1,24 @@
+import { Hono } from "hono"
+import { getDB } from "../database"
+import { getAccessToken } from "../services/rcs-service"
+const route = new Hono<Environment>()
+
+route.post("/hook", async c => {
+    console.log(await c.req.json())
+    return c.json({})
+})
+
+route.get("/addTester", async c => {
+    return c.html(`<form action="/rcs/addTester" method="post">
+    <input name="number" />
+    <button type="submit">add</button>
+    </form>`)
+})
+
+route.post("/addTester", async c => {
+    const form = await c.req.formData()
+    const number = form.get("number")
+    await getAccessToken()
+    return c.html(`a message has been sent to ${number}`)
+})
+export default route

+ 27 - 0
src/services/rcs-service.ts

@@ -0,0 +1,27 @@
+import { Buffer } from "node:buffer"
+
+const config = {
+    clientId: "ZBtzSyi7jOThVQVA",
+    clientName: "mesgity_7JOlyTGV",
+    clientSecret: "wrk1XgAn2UXAsqz7pWjdqpjnXYxEsWsa",
+    botId: "ZBtzSyi7jOThVQVA",
+    apiBaseUrl: "https://api.dotgo.com/"
+}
+
+export const getAccessToken = async () => {
+    const response = await fetch(
+        "https://api.dotgo.com/auth/oauth/token?grant_type=client_credentials",
+        {
+            method: "POST",
+            headers: {
+                Authorization: `Basic ${Buffer.from(
+                    `${config.clientId}:${config.clientSecret}`
+                ).toString("base64")}`
+            }
+        }
+    )
+    const result = await response.json()
+    console.log(result)
+}
+
+export const sendMessage = async (number: string, msg: string) => {}

+ 3 - 2
tsconfig.json

@@ -8,9 +8,10 @@
       "ESNext"
     ],
     "types": [
-      "@cloudflare/workers-types"
+      "@cloudflare/workers-types",
+      "node"
     ],
     "jsx": "react-jsx",
-    "jsxImportSource": "hono/jsx"
+    "jsxImportSource": "hono/jsx",
   },
 }

+ 1 - 0
wrangler.toml

@@ -1,5 +1,6 @@
 name = "mesgity"
 compatibility_date = "2023-12-01"
+compatibility_flags = [ "nodejs_compat" ]
 
 [vars]
 JWT_SECRET="RfR*P!P^9@suU&"