Skip to content

Commit daeeadf

Browse files
author
Tony Bedford
authored
Improve code snippets (#81)
* Updated * Clean error logging
1 parent 218393f commit daeeadf

14 files changed

+152
-109
lines changed

dispatch/send-failover-facebook-sms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ nexmo.dispatch.create(
5050
],
5151
(err, data) => {
5252
if (err) {
53-
console.log("logging after error !!!", err);
53+
console.error(err);
5454
} else {
5555
console.log(data.dispatch_uuid);
5656
}

dispatch/send-failover-mms-sms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ nexmo.dispatch.create(
4949
],
5050
(err, data) => {
5151
if (err) {
52-
console.log("logging after error !!!");
52+
console.error(err);
5353
} else {
5454
console.log(data.dispatch_uuid);
5555
}

dispatch/send-failover-sms-sms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ nexmo.dispatch.create(
4949
],
5050
(err, data) => {
5151
if (err) {
52-
console.log("logging after error !!!", err);
52+
console.error(err);
5353
} else {
5454
console.log(data.dispatch_uuid);
5555
}

dispatch/send-failover-viber-sms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ nexmo.dispatch.create(
4949
],
5050
(err, data) => {
5151
if (err) {
52-
console.log("logging after error !!!", err);
52+
console.error(err);
5353
} else {
5454
console.log(data.dispatch_uuid);
5555
}

dispatch/send-failover-whatsapp-sms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ nexmo.dispatch.create(
4949
],
5050
(err, data) => {
5151
if (err) {
52-
console.log("logging after error !!!", err);
52+
console.error(err);
5353
} else {
5454
console.log(data.dispatch_uuid);
5555
}

messages/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package.json
3+
package-lock.json

messages/messenger/send-text.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('dotenv').config({path: __dirname + '/../../.env'})
2+
3+
const NEXMO_API_KEY = process.env.NEXMO_API_KEY
4+
const NEXMO_API_SECRET = process.env.NEXMO_API_SECRET
5+
const NEXMO_APPLICATION_ID = process.env.NEXMO_APPLICATION_ID
6+
const NEXMO_APPLICATION_PRIVATE_KEY_PATH = __dirname +"/../../"+ process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH
7+
8+
const FB_RECIPIENT_ID = process.env.FB_RECIPIENT_ID
9+
const FB_SENDER_ID = process.env.FB_SENDER_ID
10+
const BASE_URL = process.env.BASE_URL
11+
12+
const Nexmo = require('nexmo')
13+
14+
const nexmo = new Nexmo({
15+
apiKey: NEXMO_API_KEY,
16+
apiSecret: NEXMO_API_SECRET,
17+
applicationId: NEXMO_APPLICATION_ID,
18+
privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
19+
}, {
20+
apiHost: BASE_URL
21+
})
22+
23+
nexmo.channel.send(
24+
{ "type": "messenger", "id": FB_RECIPIENT_ID },
25+
{ "type": "messenger", "id": FB_SENDER_ID },
26+
{
27+
"content": {
28+
"type": "text",
29+
"text": "This is a Facebook Messenger text message sent using the Messages API"
30+
}
31+
},
32+
(err, data) => {
33+
if (err) {
34+
console.error(err);
35+
} else {
36+
console.log(data.message_uuid);
37+
}
38+
}
39+
);

messages/send-facebook-messenger.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

messages/send-mms.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
require('dotenv').config({path: __dirname + '/../.env'})
22

3-
const TO_NUMBER = process.env.TO_NUMBER
4-
const NEXMO_NUMBER = process.env.NEXMO_NUMBER
5-
63
const NEXMO_API_KEY = process.env.NEXMO_API_KEY
74
const NEXMO_API_SECRET = process.env.NEXMO_API_SECRET
85
const NEXMO_APPLICATION_ID = process.env.NEXMO_APPLICATION_ID
96
const NEXMO_APPLICATION_PRIVATE_KEY_PATH = __dirname +"/../"+ process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH
107

8+
const TO_NUMBER = process.env.TO_NUMBER
9+
const NEXMO_NUMBER = process.env.FROM_NUMBER
10+
const IMAGE_URL = process.env.IMAGE_URL
11+
1112
const Nexmo = require('nexmo')
1213

1314
const nexmo = new Nexmo({
@@ -18,13 +19,19 @@ const nexmo = new Nexmo({
1819
})
1920

2021
nexmo.channel.send(
21-
{ "type": "mms", "number": "TO_NUMBER" },
22-
{ "type": "mms", "number": "FROM_NUMBER" },
22+
{ "type": "mms", "number": TO_NUMBER },
23+
{ "type": "mms", "number": FROM_NUMBER },
2324
{
2425
"content": {
2526
"type": "image",
26-
"image": { "url": "IMG_URL" }
27+
"image": { "url": IMAGE_URL }
2728
}
2829
},
29-
(err, data) => { console.log(data.message_uuid); }
30+
(err, data) => {
31+
if (err) {
32+
console.error(err);
33+
} else {
34+
console.log(data.message_uuid);
35+
}
36+
}
3037
);

messages/send-sms.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require('dotenv').config({path: __dirname + '/../.env'})
22

3-
const TO_NUMBER = process.env.TO_NUMBER
4-
const NEXMO_NUMBER = process.env.NEXMO_NUMBER
5-
63
const NEXMO_API_KEY = process.env.NEXMO_API_KEY
74
const NEXMO_API_SECRET = process.env.NEXMO_API_SECRET
85
const NEXMO_APPLICATION_ID = process.env.NEXMO_APPLICATION_ID
96
const NEXMO_APPLICATION_PRIVATE_KEY_PATH = __dirname +"/../"+ process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH
107

8+
const TO_NUMBER = process.env.TO_NUMBER
9+
const NEXMO_NUMBER = process.env.FROM_NUMBER
10+
1111
const Nexmo = require('nexmo')
1212

1313
const nexmo = new Nexmo({
@@ -18,13 +18,19 @@ const nexmo = new Nexmo({
1818
})
1919

2020
nexmo.channel.send(
21-
{ "type": "sms", "number": "TO_NUMBER" },
22-
{ "type": "sms", "number": "FROM_NUMBER" },
21+
{ "type": "sms", "number": TO_NUMBER },
22+
{ "type": "sms", "number": FROM_NUMBER },
2323
{
2424
"content": {
2525
"type": "text",
26-
"text": "This is an SMS sent from the Messages API"
26+
"text": "This is an SMS text message sent using the Messages API"
2727
}
2828
},
29-
(err, data) => { console.log(data.message_uuid); }
29+
(err, data) => {
30+
if (err) {
31+
console.error(err);
32+
} else {
33+
console.log(data.message_uuid);
34+
}
35+
}
3036
);

messages/send-viber.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

messages/send-whatsapp.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

messages/viber/send-text.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('dotenv').config({path: __dirname + '/../../.env'})
2+
3+
const NEXMO_API_KEY = process.env.NEXMO_API_KEY
4+
const NEXMO_API_SECRET = process.env.NEXMO_API_SECRET
5+
const NEXMO_APPLICATION_ID = process.env.NEXMO_APPLICATION_ID
6+
const NEXMO_APPLICATION_PRIVATE_KEY_PATH = __dirname +"/../../"+ process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH
7+
8+
const TO_NUMBER = process.env.TO_NUMBER
9+
const VIBER_SERVICE_MESSAGE_ID = process.env.VIBER_SERVICE_MESSAGE_ID
10+
const BASE_URL = process.env.BASE_URL
11+
12+
const Nexmo = require('nexmo')
13+
14+
const nexmo = new Nexmo({
15+
apiKey: NEXMO_API_KEY,
16+
apiSecret: NEXMO_API_SECRET,
17+
applicationId: NEXMO_APPLICATION_ID,
18+
privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
19+
}, {
20+
apiHost: BASE_URL
21+
})
22+
23+
nexmo.channel.send(
24+
{ "type": "viber_service_msg", "number": TO_NUMBER },
25+
{ "type": "viber_service_msg", "id": VIBER_SERVICE_MESSAGE_ID },
26+
{
27+
"content": {
28+
"type": "text",
29+
"text": "This is a Viber Service Message text message sent using the Messages API"
30+
}
31+
},
32+
(err, data) => {
33+
if (err) {
34+
console.error(err);
35+
} else {
36+
console.log(data.message_uuid);
37+
}
38+
}
39+
);

messages/whatsapp/send-text.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('dotenv').config({path: __dirname + '/../../.env'})
2+
3+
const NEXMO_API_KEY = process.env.NEXMO_API_KEY
4+
const NEXMO_API_SECRET = process.env.NEXMO_API_SECRET
5+
const NEXMO_APPLICATION_ID = process.env.NEXMO_APPLICATION_ID
6+
const NEXMO_APPLICATION_PRIVATE_KEY_PATH = __dirname +"/../../"+ process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH
7+
8+
const TO_NUMBER = process.env.TO_NUMBER
9+
const WHATSAPP_NUMBER = process.env.WHATSAPP_NUMBER
10+
const BASE_URL = process.env.BASE_URL
11+
12+
const Nexmo = require('nexmo')
13+
14+
const nexmo = new Nexmo({
15+
apiKey: NEXMO_API_KEY,
16+
apiSecret: NEXMO_API_SECRET,
17+
applicationId: NEXMO_APPLICATION_ID,
18+
privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
19+
}, {
20+
apiHost: BASE_URL
21+
})
22+
23+
nexmo.channel.send(
24+
{ "type": "whatsapp", "number": TO_NUMBER },
25+
{ "type": "whatsapp", "number": WHATSAPP_NUMBER },
26+
{
27+
"content": {
28+
"type": "text",
29+
"text": "This is a WhatsApp Message text message sent using the Messages API"
30+
}
31+
},
32+
(err, data) => {
33+
if (err) {
34+
console.error(err);
35+
} else {
36+
console.log(data.message_uuid);
37+
}
38+
}
39+
);

0 commit comments

Comments
 (0)