Created
May 3, 2022 23:24
-
-
Save maria-statsig/18d30500c1a8267d2edf9992a1e9e1b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NextApiRequest, NextApiResponse } from 'next'; | |
import { getSession } from 'next-auth/react'; | |
import { genValidateSession } from '../../lib/SessionUtils'; | |
import axios from 'axios'; | |
import { IDUtils } from '../../lib/IDUtils'; | |
import Secrets from '../../lib/Secrets' | |
import statsig, { StatsigOptions } from 'statsig-node'; | |
export default async function marcosbot_workplace( | |
req: NextApiRequest, | |
res: NextApiResponse | |
): Promise<void> { | |
// 1. Handle verification of webhook | |
if (req.method === 'GET') { | |
// Your verify token. Should be a random string. | |
let VERIFY_TOKEN = '******'; | |
// Parse the query params | |
let mode = req.query['hub.mode']; | |
let token = req.query['hub.verify_token']; | |
let challenge = req.query['hub.challenge']; | |
// Checks if a token and mode is in the query string of the request | |
if (mode && token) { | |
// Checks the mode and token sent is correct | |
if (mode === 'subscribe' && token === VERIFY_TOKEN) { | |
// Responds with the challenge token from the request | |
console.log('WEBHOOK_VERIFIED'); | |
res.status(200).send(challenge); | |
} else { | |
// Responds with '403 Forbidden' if verify tokens do not match | |
res.status(403); | |
} | |
return; | |
} | |
} | |
// You can find this in the Details section of your integration | |
const access_token = "****"; | |
// 2. Get message | |
let msg: string | undefined; | |
let data = req.body; | |
let thread_id: any = {}; | |
if (data.object == 'page') { | |
for (let pageEntry of data.entry) { | |
for (let messagingEvent of pageEntry.messaging) { | |
thread_id = messagingEvent.thread?.id; | |
if (messagingEvent.message) msg = messagingEvent.message.text ?? ''; | |
} | |
} | |
} | |
msg = msg ?? ''; | |
if (msg === '') { | |
res.status(200).json({ | |
success: true, | |
}); | |
return; | |
} | |
// 2. Parse message | |
let marco_name: any = {}; | |
let marco_command = ''; | |
try { | |
let marco_parsed = msg.split(" ", 2); | |
marco_command = marco_parsed[0] | |
marco_name = marco_parsed[1]; | |
// Our bot responds whenever #m, @MarcosBot, or MarcosBot is used | |
if (!(marco_command === "#m" || marco_command === "@MarcosBot" || marco_command === "MarcosBot")) { | |
res.status(200).json({ | |
success: true, | |
}); | |
return; | |
} | |
} catch (e) { | |
res.status(200).json({ | |
success: true, | |
}); | |
return; | |
} | |
// 4. Use Statsig to retrieve image url | |
await statsig.initialize(Secrets.StatsigInternServerSecretKey, { environment: { tier: "development" } }); | |
const marcos = (await statsig.getConfig({ | |
userID: "workplace", | |
}, "marcos")).value as Record<string, unknown>; | |
let marco = marcos[marco_name] as ({ src: string } | undefined); | |
if (!marco) { | |
marco = marcos['brokenmarcosbot'] as { src: string }; | |
} | |
// 5. Send response | |
let messageData = { | |
message_type: "RESPONSE", | |
recipient: { | |
thread_key: thread_id | |
}, | |
message: { | |
attachment: { | |
type: "image", | |
payload: { | |
is_reusable: true, | |
url: marco.src | |
} | |
} | |
}, | |
}; | |
try { | |
const response = await axios.post( | |
`https://graph.facebook.com/v13.0/me/messages?access_token=${access_token}`, | |
messageData, | |
{ | |
headers: { | |
Authorization: `Bearer ${access_token}`, | |
'Content-Type': 'application/json', | |
}, | |
}, | |
); | |
statsig.logEvent({ | |
userID: "workplace", | |
}, "parse:" + (marco_name ?? ''), undefined, marco); | |
} catch (e) { | |
console.error(e); | |
} | |
res.status(200).json({ | |
success: true, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment