Skip to content

Instantly share code, notes, and snippets.

@ptrckbp
Last active January 26, 2023 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ptrckbp/b3eb2d956b7f999054211908a9ff27cd to your computer and use it in GitHub Desktop.
Save ptrckbp/b3eb2d956b7f999054211908a9ff27cd to your computer and use it in GitHub Desktop.
Adding Dashbot to Botpress
const DEBUG_DEV = false // set to true to test without setting EXPOSED_EXTERNAL_ANALYTICS=true in your environment
const DASHBOT_KEY = '165agsuvsjRfYOMXlenCCx3RzWc9wTAbMtbVGKn9' // change this to your Dashbot API key.
const DASHBOT_INBOUND_LINK = `https://tracker.dashbot.io/track?platform=universal&v=11.1.0-rest&type=incoming&apiKey=${DASHBOT_KEY}`
const DASHBOT_UNHANDLED_INTENT = 'NotHandled'
const axios = require('axios')
// Modify this if you are tracking authenticated user.
const getUserMeta = () => {
return {
userId: event.target // by default we grab the conversation target id
}
}
const { userId } = getUserMeta()
const sendAnalytics = data => {
axios.post(DASHBOT_INBOUND_LINK, {
...data,
userId,
dashbot_timestamp: Date.now(),
platformJson: {
receivedAtSMs: new Date().getUTCSeconds() + '.' + new Date().getUTCMilliseconds()
}
})
}
const getInputs = () => {
return Object.keys(event.nlu.slots).map(slotName => {
return { name: slotName, value: event.nlu.slots[slotName].value }
})
}
const getIntent = () => {
const intent = event.nlu.intent
return {
name: intent.name === 'none' ? DASHBOT_UNHANDLED_INTENT : intent.name,
confidence: intent.confidence,
inputs: getInputs()
}
}
const handlePureText = () => {
return sendAnalytics({ text: event.payload.text, intent: getIntent() })
}
const handleQuickReply = () => {
return sendAnalytics({ text: event.payload.text })
}
const handleOutlier = () => {
return sendAnalytics({ text: `Special message : ${JSON.stringify(event.payload, null, '\t')}` })
}
const main = async () => {
if (!DEBUG_DEV && !process.env.EXPOSED_EXTERNAL_ANALYTICS) {
// remove this statement if you want to always have analytics
return
}
if (event.type === 'text') {
return handlePureText()
}
if (event.type === 'quick_reply') {
return handleQuickReply()
}
handleOutlier()
}
main()
const DEBUG_DEV = false // set to true to test without setting EXPOSED_EXTERNAL_ANALYTICS=true in your environment
const DASHBOT_KEY = '165agsuvsjRfYOMXlenCCx3RzWc9wTAbMtbVGKn9' // change this to your Dashbot API key.
const DASHBOT_OUTBOUND_LINK = `https://tracker.dashbot.io/track?platform=universal&v=11.1.0-rest&type=outgoing&apiKey=${DASHBOT_KEY}`
const axios = require('axios')
// Modify this if you are tracking authenticated user.
const getUserMeta = () => {
return {
userId: event.target // by default we grab the conversation target id
}
}
const { userId } = getUserMeta()
const sendAnalytics = data => {
axios.post(DASHBOT_OUTBOUND_LINK, {
...data,
userId,
dashbot_timestamp: Date.now(),
platformJson: {
receivedAtSMs: new Date().getUTCSeconds() + '.' + new Date().getUTCMilliseconds()
}
})
}
const handlePureText = () => {
return sendAnalytics({ text: event.payload.text })
}
const handleSingleChoice = () => {
return sendAnalytics({
text: event.payload.text,
buttons: event.payload.choices.map(choice => {
return {
label: choice.title,
value: choice.value
}
})
})
}
const handleImage = () => {
return sendAnalytics({
text: '',
images: [{ url: event.payload.image }]
})
}
const handleOutlier = () => {
return sendAnalytics({ text: `Special message response : ${JSON.stringify(event.payload, null, '\t')}` })
}
const main = async () => {
if (!DEBUG_DEV && !process.env.EXPOSED_EXTERNAL_ANALYTICS) {
// remove this statement if you want to always have analytics
return
}
if (event.type === 'text') {
return handlePureText()
}
if (event.type === 'single-choice') {
return handleSingleChoice()
}
if (event.type === 'image') {
return handleImage()
}
handleOutlier()
}
main()
bp.logger.info(JSON.stringify(bp.config.getBotpressConfig()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment