@broid/messenger
Version:
Convert Facebook Messenger messages into Activity Streams 2 with Broid Integration
218 lines (213 loc) • 6.11 kB
JavaScript
const BroidSchemas = require("@broid/schemas");
const uuid = require("uuid");
const uuidv4 = uuid.v4;
function newFakeActivityStream(actor, to) {
return {
"@context": "https://www.w3.org/ns/activitystreams",
"actor": {
id: actor.id,
name: actor.name,
type: "Application"
},
"generator": {
id: "webmessenger-backend",
name: "webmessenger-backend",
type: "Service"
},
"object": {
content: "",
id: uuidv4(),
type: "Note"
},
"published": Math.round((new Date()).getTime() / 1000),
"to": {
id: to.id,
name: to.name,
type: "Person"
},
"type": "Create"
};
}
exports.sendFakeMessage = function(chatMessage) {
const message = newFakeActivityStream(chatMessage.target, chatMessage.actor);
message.object.content = "Hello from the server!";
const content = chatMessage.object.content || "";
switch (content.toLowerCase()) {
case "simple_message_with_buttons":
message.object.content = "a simple message with buttons";
message.object.attachment = [
{
"type": "Button",
"content": "Broid's website",
"name": "broid",
"mediaType": "text/html",
"url": "https://www.broid.ai"
},
{
"type": "Button",
"content": "PostbackContent",
"name": "PostbackName",
"url": "PostbackURL"
}
];
break;
case "simple_message_with_button":
message.object.content = "a simple message with button";
message.object.attachment = [
{
"type": "Button",
"content": "Broid's website",
"name": "broid",
"mediaType": "text/html",
"url": "https://www.broid.ai"
}
];
break;
case "quickreply":
message.object.content = "Pick the better company (hint, it\"s the first one.)";
message.object.attachment = [
{
"type": "Button",
"content": "Broid",
"url": "broid_payload",
"name": "broid",
"mediaType": "text/plain",
},
{
"type": "Button",
"content": "Google",
"url": "google_payload",
"name": "google",
"mediaType": "text/plain"
},
{
"type": "Button",
"content": "Locate me",
"url": "google_payload",
"mediaType": "application/vnd.geo+json",
"name": "locaion"
}
];
break;
case "card":
message.object = {
"type": "Image",
"content": "example of simple card",
"name": "Card name",
"mediaType": "image/jpeg",
"url": "http://i.imgur.com/NonIvyO.png",
"attachment": []
};
break;
case "card_buttons":
message.object = {
"type": "Image",
"content": "example of simple card",
"name": "Card name",
"mediaType": "image/jpeg",
"url": "http://i.imgur.com/NonIvyO.png",
"attachment": [{
"type": "Button",
"mediaType": "text/html",
"content": "Try it now!",
"name": "Try it now!",
"url": "https://www.broid.ai/waiting-list"
},
{
"type": "Button",
"name": "postback_content",
"content": "Try it the postback!",
"url": "payload_postback"
},
{
"type": "Button",
"mediaType": 'audio/telephone-event',
"content": "Call me",
"name": "Call me",
"url": "+5144456767"
},
// EXPERIMENTAL
// {
// "type": "Button",
// "mediaType": 'broid/share',
// "content": "Share this card",
// "name": "Share this card",
// "url": "share"
// }
]
};
break;
case "carousel":
message.object = {
"type": "Collection",
"items": [
{
"type": "Image",
"name": "One channel to rule them all",
"content": "With Broid, you can converse on more than 20 messaging channels in a single action!",
"url": "http://i.imgur.com/OmoV6QR.png",
"attachment": []
},
{
"type": "Image",
"name": "Another Cool Card",
"content": "With a description",
"url": "http://i.imgur.com/rzFtIeh.gif",
"attachment": []
}
]
};
break;
case "carousel_buttons":
message.object = {
"type": "Collection",
"items": [
{
"type": "Image",
"name": "One channel to rule them all",
"content": "With Broid, you can converse on more than 20 messaging channels in a single action!",
"url": "http://i.imgur.com/OmoV6QR.png",
"attachment": [{
"type": "Button",
"mediaType": "text/html",
"content": "Try it now!",
"name": "Try it now!",
"url": "https://www.broid.ai/waiting-list"
}]
},
{
"type": "Image",
"name": "Another Cool Card",
"content": "With a description",
"url": "http://i.imgur.com/rzFtIeh.gif",
"attachment": [{
"type": "Button",
"mediaType": "text/html",
"content": "And a button",
"name": "And a button",
"url": "https://www.broid.ai/waiting-list"
}]
}
]
};
break;
case "typing/on":
message.object = {
content: 'typing/on',
type: 'Activity',
id: uuidv4(),
};
break;
case "typing/off":
message.object = {
content: 'typing/off',
type: 'Activity',
id: uuidv4(),
};
break;
default:
message.object.content = "Hello from the server!";
break;
}
return message;
}