UNPKG

@broid/messenger

Version:

Convert Facebook Messenger messages into Activity Streams 2 with Broid Integration

202 lines (192 loc) 5.77 kB
const BroidMessenger = require('../lib/core'); const { Observable } = require('rxjs/Rx'); const { sendFakeMessage } = require('./fake'); const messenger = new BroidMessenger({ token: "EAALsZCH5FACABAOMka2TwdG1EpPPR0EE5cKZCooZBVkq8laQHZAaINM2dswZC6GmWmhLZCwalgWoB77tkDYm4VHP8vBJtu38MAx2eFRjDxM3BsVrdrzZBgTE8ka8UiSvIpzjBCckUdGCTRa4Q9VmoTJPZBM1HYiimeUqyWlg79lhMQZDZD", tokenSecret: "verify_token", logLevel: 'debug', // consumerSecret: "1c6ffd78c92a32c0e8f515a06108eb2f", http: { port: 8080, host: "0.0.0.0" } }); let replied = false; Observable.merge(messenger.connect(), messenger.listen()) .subscribe({ next: (event) => { console.log(event); if (!event["@context"]) { return; } if (!replied) { replied = true; const m = sendFakeMessage(event); console.log('Received:', m); const reset = () => setTimeout(() => replied = false, 700); messenger.send(m) .then(reset) .catch((e) => { console.error(e); reset(); }); } }, error: (err) => console.error(`Something went wrong: ${err.message}`), complete: () => console.log('complete'), }); // // CAROUSSEL // messenger.send({ // "@context": "https://www.w3.org/ns/activitystreams", // "generator": { // "id": "155502342883643954", // "name": "messenger", // "type": "Service" // }, // "object": { // "type": "Collection", // "items": [ // { // "type": "Image", // "name": "Product A", // "content": "a cool product a", // "url": "https://unsplash.it/200/300", // "attachment": [{ // "type": "Button", // "content": "Broid's website", // "name": "broid", // "mediaType": "text/html", // "url": "https://www.broid.ai" // }, { // "type": "Button", // "content": "Add to cart", // "name": "Add to cart", // "url": "action=buy&itemid=111" // }] // }, // { // "type": "Image", // "name": "Product B", // "content": "a cool product b", // "url": "https://unsplash.it/g/200/300", // "attachment": [{ // "type": "Button", // "content": "Broid's website", // "name": "broid", // "mediaType": "text/html", // "url": "https://www.broid.ai" // }, { // "type": "Button", // "content": "Add to cart", // "name": "Buy this product", // "url": "action=buy&itemid=222" // }] // } // ] // }, // "to": { // "id": "1339832819411780", // "type": "Person" // }, // // "type": "Create" // }); // // messenger.send({ // "@context": "https://www.w3.org/ns/activitystreams", // "generator": { // "id": "155502342883643954", // "name": "messenger", // "type": "Service" // }, // "object": { // "type": "Image", // "name": "Product B", // "content": "a cool product b", // "url": "https://unsplash.it/g/200/300", // "attachment": [{ // "type": "Button", // "content": "Broid's website", // "name": "broid", // "mediaType": "text/html", // "url": "https://www.broid.ai" // }, { // "type": "Button", // "content": "Add to cart", // "name": "Buy this product", // "url": "action=buy&itemid=222" // }] // }, // "to": { // "id": "1339832819411780", // "type": "Person" // }, // // "type": "Create" // }); // // // // messenger.send( // { // "@context": "https://www.w3.org/ns/activitystreams", // "actor": { // "id": "e45b0a39f3784aab9f4cd6fb6b026c27", // "name": "WebMessenger User e45b0a39f3784aab9f4cd6fb6b026c27", // "type": "Person" // }, // "generator": { // "id": "154588969244824114", // "name": "webmessenger", // "type": "Service" // }, // "object": { // "attachment": [{ // "content": "Take the Tour", // "name": "Take the Tour", // "type": "Button", // "url": "Take the Tour" // }, { // "content": "Whisper", // "name": "Whisper", // "type": "Button", // "url": "Whisper" // }, { // "content": "Cards", // "name": "Cards", // "type": "Button", // "url": "Cards" // }, { // "content": "Carousel", // "name": "Carousel", // "type": "Button", // "url": "Carousel" // }, { // "content": "Quick Replies", // "name": "Quick Replies", // "type": "Button", // "url": "Quick Replies" // }, { // "content": "Channels", // "name": "Channels", // "type": "Button", // "url": "Channels" // }], // "content": "You can either let me guide through them or jump directly to one of the features below. At anytime, you can type “menu” to go back here.", // "id": "205b066c-6200-4bfd-92f2-4ae9c82e572a", // "type": "Note" // }, // "published": 1505745214, // "target": { // "id": "broidapi", // "name": "broidapi", // "type": "Application" // }, // "to": { // "id": "e45b0a39f3784aab9f4cd6fb6b026c27", // "name": "WebMessenger User e45b0a39f3784aab9f4cd6fb6b026c27", // "type": "Person" // }, // "type": "Create" // } // );