Skip to content

Commit

Permalink
update sendmammy
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Aug 12, 2021
1 parent e817cdc commit 7d3917e
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 236 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
"author": "Adhiraj Singh",
"license": "ISC",
"dependencies": {
"@types/mustache": "^4.1.1",
"got": "^11.8.1",
"he": "^1.2.0",
"mustache": "^4.1.0",
"natural": "^2.1.5"
},
"peerDependencies": {
"@adiwajshing/baileys": "^3.4.0",
"@chatdaddy/authentication-utils": "git+https://github.com/chatdaddy/authentication-utils"
"@chatdaddy/service-auth-client": "git+https://github.com/chatdaddy/service-auth-client"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.82",
"@types/mustache": "^4.1.1",
"@adiwajshing/baileys": "^3.4.0",
"@chatdaddy/authentication-utils": "git+https://github.com/chatdaddy/authentication-utils",
"@chatdaddy/service-auth-client": "git+https://github.com/chatdaddy/service-auth-client",
"@types/he": "^1.1.1",
"@types/natural": "^2.1.1",
"@types/node": "^14.14.31",
Expand Down
44 changes: 21 additions & 23 deletions src/SendMammyResponder.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { WAMessage } from "@adiwajshing/baileys"
import type { AuthenticationController } from "@chatdaddy/authentication-utils"
import { Answer, LanguageProcessor, WAResponderParameters } from "./types"
import { onWAMessage } from "./WAResponder"
import type { makeAccessTokenFactory, Scope, JWT } from "@chatdaddy/service-auth-client"
import type { APIGatewayProxyEvent } from "aws-lambda"
import got from "got"
import { URL } from "url"
import { Answer, LanguageProcessor, WAResponderParameters } from "./types"
import { onWAMessage } from "./WAResponder"

// serverless function for interacting with SendMammy APIs

export const SENDMAMMY_URL = new URL(process.env.SENDMAMMY_URL || 'https://api.sendmammy.com')

export type SendMammyResponderParameters = WAResponderParameters & ({
export type SendMammyResponderParameters = WAResponderParameters & {
refreshToken: string
} | {
authenticationController: AuthenticationController
})
}
export type SendMessageOptions = {
jid: string
answer: Answer
Expand Down Expand Up @@ -64,30 +63,29 @@ export const sendWAMessage = async({ jid, answer, quotedId, token }: SendMessage
}
}

const { makeAuthenticationController } = require('@chatdaddy/authentication-utils') || {}
const authClient = require('@chatdaddy/service-auth-client')
export const createSendMammyResponder = (
processor: LanguageProcessor,
metadata: SendMammyResponderParameters
) => {
if(!makeAuthenticationController) {
throw new Error('Could not find @chatdaddy/authentication-utils')
}
let authController: AuthenticationController
if('authenticationController' in metadata) {
authController = metadata.authenticationController
} else {
authController = makeAuthenticationController(
metadata.refreshToken,
process.env.AUTH_SERVICE_URL || 'https://api-auth.chatdaddy.tech'
)
if(!authClient) {
throw new Error('Could not find @chatdaddy/service-auth-client')
}
const factory = authClient.makeAccessTokenFactory as typeof makeAccessTokenFactory
const Scopes = authClient.Scope as typeof Scope
const getToken = factory({
request: {
refreshToken: metadata.refreshToken,
scopes: [Scopes.MessagesSendToAll]
},
})

return async (event: any) => {
return async (event: APIGatewayProxyEvent) => {
const authToken = event.headers['Authorization']?.replace('Bearer ', '')
const user = await authController.authenticate(authToken)
const { user }: JWT = await authClient.verifyToken(authToken)

const sendMessage = async(jid: string, answer: Answer, quoted?: WAMessage) => {
const token = await authController.getToken(user.teamId)
const { token } = await getToken(user.teamId)
const result = await sendWAMessage({ jid, answer, quotedId: quoted?.key.id, token })
return result
}
Expand All @@ -110,7 +108,7 @@ export const createSendMammyResponder = (
body: JSON.stringify({ success: true })
}
} else if(body.event === 'initial-data-received') {
const token = await authController.getToken(user.teamId)
const token = await authClient.getToken(user.teamId)
// get all unread chats
const result = await got.get(
new URL(`chats?unread=true`, SENDMAMMY_URL),
Expand Down
Loading

0 comments on commit 7d3917e

Please sign in to comment.