UNPKG

2.65 kBJavaScriptView Raw
1var Promise = require('promise');
2var winston = require('./winston');
3
4var Message = function(config, single, plural){
5 var request = require('./c21request.js')(config)
6
7 return {
8
9
10 send: function(sender_fullname, recipient_id, recipient_fullname, text, sender_id, attributes, type, metadata){
11 var message = {}
12 if (sender_id) {
13 message["sender_id"] = sender_id;
14 }
15 message["sender_fullname"] = sender_fullname;
16 message["recipient_id"] = recipient_id;
17 message["recipient_fullname"] = recipient_fullname;
18 message["text"] = text;
19
20 if (attributes) {
21 message["attributes"] = attributes;
22 }
23
24 if (type) {
25 message["type"] = type;
26 }
27
28 if (metadata) {
29 message["metadata"] = metadata;
30 }
31
32 return new Promise(function(resolve, reject){
33
34 var admintoken = "";
35 if (config.admintoken) {
36 admintoken = "?token=" + encodeURIComponent(config.admintoken);
37 winston.debug("chat21-node-sdk admintoken", admintoken);
38 }
39
40 request.post('/' + plural + admintoken, message).then(function(data){
41 resolve(data)
42 }).catch(function(err){
43 reject(err)
44 })
45 })
46 },
47 sendToGroup: function(sender_fullname, recipient_id, recipient_fullname, text, sender_id, attributes, type, metadata, timestamp){
48 var message = {}
49 if (sender_id) {
50 message["sender_id"] = sender_id;
51 }
52 message["sender_fullname"] = sender_fullname;
53 message["recipient_id"] = recipient_id;
54 message["channel_type"] = "group";
55 message["recipient_fullname"] = recipient_fullname;
56 message["text"] = text;
57
58 if (attributes) {
59 message["attributes"] = attributes;
60 }
61
62 if (type) {
63 message["type"] = type;
64 }
65
66
67 if (metadata) {
68 message["metadata"] = metadata;
69 }
70 if (timestamp) {
71 message["timestamp"] = timestamp;
72 }
73
74
75
76 winston.debug("chat21-node-sdk message.sendToGroup", message);
77 return new Promise(function(resolve, reject){
78
79 var admintoken = "";
80 if (config.admintoken) {
81 admintoken = "?token=" + encodeURIComponent(config.admintoken);
82 }
83
84 request.post('/' + plural + admintoken, message).then(function(data){
85 winston.debug("chat21-node-sdk message.sent", data);
86 resolve(data)
87 }).catch(function(err){
88 winston.error("chat21-node-sdk message.sent error", err);
89 reject(err)
90 })
91 })
92 },
93
94
95 }
96}
97
98module.exports = Message
\No newline at end of file