Created
September 30, 2019 13:25
-
-
Save spasiu/3020017122dc032ea7fefcf6dcf5f7bc to your computer and use it in GitHub Desktop.
Example of handling delivery events
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 express = require('express'); | |
| const bodyParser = require('body-parser'); | |
| const Smooch = require('smooch-core'); | |
| const APP_ID = ''; | |
| const KEY_ID = ''; | |
| const SECRET = ''; | |
| const outboundMessageLogs = []; | |
| const smooch = new Smooch({ | |
| scope: 'account', | |
| keyId: KEY_ID, | |
| secret: SECRET | |
| }); | |
| const app = express(); | |
| app.listen(8000); | |
| app.use(bodyParser.json()); | |
| app.get('/', getIndexHandler); | |
| app.post('/events', eventHandler); | |
| function getIndexHandler(req, res) { | |
| res.send(` | |
| <pre>${JSON.stringify(outboundMessageLogs, null, 4)}</pre> | |
| <script> | |
| !function(e,n,t,r){ | |
| function o(){try{var e;if((e="string"==typeof this.response?JSON.parse(this.response):this.response).url){ | |
| var t=n.getElementsByTagName("script")[0],r=n.createElement("script");r.async=!0,r.src=e.url,t.parentNode.insertBefore(r,t)}}catch(e){}} | |
| var s,p,a,i=[],c=[];e[t]={init:function(){s=arguments;var e={then:function(n){return c.push({type:"t",next:n}),e},catch:function(n){ | |
| return c.push({type:"c",next:n}),e}};return e},on:function(){i.push(arguments)},render:function(){p=arguments},destroy:function(){a=arguments}}, | |
| e.__onWebMessengerHostReady__=function(n){if(delete e.__onWebMessengerHostReady__,e[t]=n,s)for(var r=n.init.apply(n,s),o=0;o<c.length;o++){ | |
| var u=c[o];r="t"===u.type?r.then(u.next):r.catch(u.next)}p&&n.render.apply(n,p),a&&n.destroy.apply(n,a);for(o=0;o<i.length;o++)n.on.apply(n,i[o])}; | |
| var u=new XMLHttpRequest;u.addEventListener("load",o),u.open("GET","https://"+r+".webloader.smooch.io/",!0),u.responseType="json",u.send() | |
| }(window,document,"Smooch", "${APP_ID}") | |
| </script> | |
| <script> | |
| Smooch.init({ appId: "${APP_ID}" }); | |
| </script> | |
| `); | |
| } | |
| async function eventHandler(req, res) { | |
| try { | |
| if (req.body.trigger === 'message:delivery:channel') { | |
| const index = outboundMessageLogs.findIndex(entry => entry.messageId === req.body.message._id); | |
| outboundMessageLogs[index] = { | |
| messageId: req.body.message._id, | |
| status: 'channel success' | |
| } | |
| } | |
| if (req.body.trigger === 'message:appUser') { | |
| for (const message of req.body.messages) { | |
| if (message.text === 'ping') { | |
| const data = await smooch.appUsers.sendMessage({ | |
| appId: req.body.app._id, | |
| userId: req.body.appUser._id, | |
| message: { | |
| text: 'pong', | |
| role: 'appMaker', | |
| type: 'text' | |
| } | |
| }); | |
| outboundMessageLogs.push({ | |
| messageId: data.message._id, | |
| status: 'na' | |
| }); | |
| } | |
| } | |
| } | |
| res.end(); | |
| } catch (err) { | |
| console.log(error); | |
| } | |
| } | |
| "OK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment