Created
April 25, 2019 15:44
-
-
Save slvnperron/7b0a4736c011b715d0c881ee2af83d31 to your computer and use it in GitHub Desktop.
Outgoing Middleware
This file contains hidden or 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
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