UNPKG

4.76 kBJavaScriptView Raw
1var sendMessageToMultiple = function (message, threads) {
2 var apis = exports.current.getIntegrationApis();
3 for (var integ in threads) {
4 if (!threads.hasOwnProperty(integ)) {
5 continue;
6 }
7 var intThreads = threads[integ];
8 for (var i = 0; i < intThreads.length; i++) {
9 apis[integ].sendMessage(message, intThreads[i]);
10 }
11 }
12 },
13
14 _loopbackWrapper = function (origionalSend, api) {
15 return function (data, thread) {
16 origionalSend(data, thread);
17 if (exports.current && exports.current.allowLoopback) {
18 let newEvent = exports.createEvent(thread, -1, 'Bot', data);
19 newEvent.event_source = 'loopback';
20 exports.current.onMessage(api, newEvent);
21 }
22 };
23 };
24
25exports.createIntegration = function (platform) {
26 if (!platform.sendMessage) {
27 platform.sendMessage = function() {
28 throw new Error($$`What kind of shit platform is this that doesn\'t even support sending messages?`);
29 };
30 }
31 platform.sendMessage = _loopbackWrapper(platform.sendMessage, platform);
32
33 if (!platform.sendUrl) {
34 platform.sendUrl = function(url, thread) {
35 platform.sendMessage(url, thread); // fallback to sending a message
36 };
37 }
38 platform.sendUrl = _loopbackWrapper(platform.sendUrl, platform);
39
40 if (!platform.sendImage) {
41 platform.sendImage = function(type, image, description, thread) {
42 switch(type) {
43 case 'url': // fallback to sending a url
44 platform.sendMessage(description, thread);
45 platform.sendUrl(image, thread);
46 break;
47 case 'file': // fallback to sending a file
48 platform.sendFile(type, image, description, thread);
49 break;
50 default: // fallback to sending a message
51 platform.sendMessage(description, thread);
52 platform.sendMessage($$`I also have something to send you but cant seem to do so...`, thread);
53 break;
54 }
55 };
56 }
57
58 if (!platform.sendFile) {
59 platform.sendFile = function(type, file, description, thread) {
60 platform.sendMessage(description, thread);
61 switch(type) {
62 case 'url': // fallback to sending a url
63 platform.sendUrl(file, thread);
64 break;
65 case 'file': // fallback to sending a message
66 platform.sendMessage($$`I have a file to send you but cant seem to do so...`, thread);
67 break;
68 default: // fallback to sending a message
69 platform.sendMessage($$`I have something to send you but cant seem to do so...`, thread);
70 break;
71 }
72 };
73 }
74
75 if (!platform.sendTyping) {
76 platform.sendTyping = function(thread) {
77 platform.sendMessage($$`Working on it...`, thread); // fallback to sending a message
78 };
79 }
80
81 if (!platform.setTitle) {
82 platform.setTitle = function(title, thread) { // fallback to sending a message
83 platform.sendMessage($$`If I could set the title of this chat I would set it to "${title}"`, thread);
84 };
85 }
86
87 if (!platform.sendPrivateMessage) {
88 platform.sendPrivateMessage = function (message, thread) {
89 platform.sendMessage(message, thread);
90 };
91 }
92
93 if (!platform.getUsers) {
94 platform.getUsers = function () {
95 return {};
96 };
97 }
98
99 if (!platform.commandPrefix) {
100 if (platform.config && platform.config.commandPrefix) {
101 platform.commandPrefix = platform.config.commandPrefix;
102 }
103 else {
104 platform.commandPrefix = '/';
105 }
106 }
107
108 platform.sendMessageToMultiple = sendMessageToMultiple;
109
110 return platform;
111};
112
113exports.createEvent = function(thread, senderId, senderName, message) {
114 var event = {
115 thread_id: thread,
116 sender_id: senderId,
117 sender_name: senderName + '', // Accept sender_name = null as a literal
118 body: message,
119 event_source: null
120 };
121 event.arguments = event.body.match(/(?:[^\s"]+|"[^"]*")+/g);
122 if (event.arguments === null) {
123 event.arguments = [''];
124 }
125 event.arguments_body = event.body.substr(event.arguments[0].length + 1);
126 for (var i = 0; i < event.arguments.length; i++) {
127 event.arguments[i] = event.arguments[i].replace(/(^["])|(["]$)/g, '');
128 }
129 return event;
130};