Skip to content

Instantly share code, notes, and snippets.

@slvnperron
Created April 25, 2019 15:44
Show Gist options
  • Save slvnperron/7b0a4736c011b715d0c881ee2af83d31 to your computer and use it in GitHub Desktop.
Save slvnperron/7b0a4736c011b715d0c881ee2af83d31 to your computer and use it in GitHub Desktop.
Outgoing Middleware
const _ = require('lodash')
/**
* This outgoing middleware will find the $"..." pattern in the ougoing text
* And transform those into suggestion chips in channel-web
* @example e.g.
* `What type of restaurant are you looking for? $“Asian” $”Mexican” $”American”`
*/
const regex = /\$(“|'|"|”)([\w\s]+)(“|'|"|”)/g
if (event.payload && typeof event.payload.text === 'string') {
const suggestions = []
event.payload.text = event.payload.text.replace(regex, function(match, g1, g2) {
suggestions.push(g2)
return ''
})
if (suggestions.length) {
event.payload = {
type: 'custom',
module: 'channel-web',
component: 'QuickReplies',
quick_replies: suggestions.map(x => ({ title: x, payload: x })),
wrapped: {
type: 'text',
..._.omit(event.payload, 'quick_replies')
}
}
event.type = 'custom'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment