Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check if sms is enabled in public registrations #599

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 101 additions & 3 deletions go-app-ussd_public_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,49 @@ go.RapidPro = function() {
return RapidPro;
}();

go.Hub = function() {
var vumigo = require('vumigo_v02');
var events = vumigo.events;
var Eventable = events.Eventable;
var url = require("url");

var Hub = Eventable.extend(function(self, json_api, base_url, auth_token) {
self.json_api = json_api;
self.base_url = base_url;
self.auth_token = auth_token;
self.json_api.defaults.headers.Authorization = ['Token ' + self.auth_token];

self.send_whatsapp_template_message = function(msisdn, template_name, media) {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;

});
};

self.get_whatsapp_failure_count = function(msisdn) {
var api_url = url.resolve(self.base_url, "/api/v2/deliveryfailure/" + msisdn + "/");

return self.json_api.get(api_url)
.then(
function(response){
return response.data.number_of_failures;
}
);
};

});
return Hub;
}();

go.app = function() {
var _ = require("lodash");
var moment = require('moment');
Expand All @@ -186,6 +229,16 @@ go.app = function() {
self.im.config.services.rapidpro.base_url,
self.im.config.services.rapidpro.token
);

self.hub = new go.Hub(
new JsonApi(self.im, {
headers: {
'User-Agent': ["Jsbox/NDoH-Clinic"]
}
}),
self.im.config.services.hub.base_url,
self.im.config.services.hub.token
);
};

self.add = function(name, creator) {
Expand Down Expand Up @@ -393,12 +446,56 @@ go.app = function() {
),
accept_labels: true,
choices: [
new Choice("state_trigger_rapidpro_flow", $("Yes")),
new Choice("state_send_welcome_template", $("Yes")),
new Choice("state_exit", $("No")),
]
});
});

self.add("state_send_welcome_template", function(name, opts) {
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
var template_name = self.im.config.welcome_template;
return self.hub
.send_whatsapp_template_message(msisdn, template_name)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
return self.states.create("state_trigger_rapidpro_flow");
}
else{
return self.states.create("state_sms_registration_not_available");
}
});
}
return self.states.create("state_trigger_rapidpro_flow");
}).catch(function(e) {
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if (opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", {
return_state: name
});
}
return self.states.create(name, opts);
});
});

self.states.add("state_sms_registration_not_available", function(name) {
return new EndState(name, {
next: "state_start",
text: $([
"It seems this number is not on WhatsApp and we don't offer SMS at this moment.",
"",
"Please register the new number on WhatsApp for the MomConnect Service."
].join("\n"))
});
});


self.states.add("state_exit", function(name) {
return new EndState(name, {
next: "state_start",
Expand All @@ -416,7 +513,8 @@ go.app = function() {
source: "Public USSD",
timestamp: new moment.utc(self.im.config.testing_today).format(),
registered_by: msisdn,
mha: 6
mha: 6,
preferred_channel: self.im.user.answers.preferred_channel
};
return self.rapidpro
.start_flow(self.im.config.flow_uuid, null, "whatsapp:" + _.trim(msisdn, "+"), data)
Expand All @@ -434,7 +532,7 @@ go.app = function() {
});

self.states.add("state_registration_complete", function(name) {
var msisdn = _.get(self.im.user.answers, "state_enter_msisdn", self.im.user.addr);
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
msisdn = utils.readable_msisdn(msisdn, "27");
var whatsapp_message = $(
"You're done! This number {{ msisdn }} will get helpful messages from MomConnect. For " +
Expand Down
104 changes: 101 additions & 3 deletions src/ussd_public_rapidpro.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
go.Hub = function() {
var vumigo = require('vumigo_v02');
var events = vumigo.events;
var Eventable = events.Eventable;
var url = require("url");

var Hub = Eventable.extend(function(self, json_api, base_url, auth_token) {
self.json_api = json_api;
self.base_url = base_url;
self.auth_token = auth_token;
self.json_api.defaults.headers.Authorization = ['Token ' + self.auth_token];

self.send_whatsapp_template_message = function(msisdn, template_name, media) {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;

});
};

self.get_whatsapp_failure_count = function(msisdn) {
var api_url = url.resolve(self.base_url, "/api/v2/deliveryfailure/" + msisdn + "/");

return self.json_api.get(api_url)
.then(
function(response){
return response.data.number_of_failures;
}
);
};

});
return Hub;
}();

go.app = function() {
var _ = require("lodash");
var moment = require('moment');
Expand All @@ -22,6 +65,16 @@ go.app = function() {
self.im.config.services.rapidpro.base_url,
self.im.config.services.rapidpro.token
);

self.hub = new go.Hub(
new JsonApi(self.im, {
headers: {
'User-Agent': ["Jsbox/NDoH-Clinic"]
}
}),
self.im.config.services.hub.base_url,
self.im.config.services.hub.token
);
};

self.add = function(name, creator) {
Expand Down Expand Up @@ -229,12 +282,56 @@ go.app = function() {
),
accept_labels: true,
choices: [
new Choice("state_trigger_rapidpro_flow", $("Yes")),
new Choice("state_send_welcome_template", $("Yes")),
new Choice("state_exit", $("No")),
]
});
});

self.add("state_send_welcome_template", function(name, opts) {
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
var template_name = self.im.config.welcome_template;
return self.hub
.send_whatsapp_template_message(msisdn, template_name)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
return self.states.create("state_trigger_rapidpro_flow");
}
else{
return self.states.create("state_sms_registration_not_available");
}
});
}
return self.states.create("state_trigger_rapidpro_flow");
}).catch(function(e) {
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if (opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", {
return_state: name
});
}
return self.states.create(name, opts);
});
});

self.states.add("state_sms_registration_not_available", function(name) {
return new EndState(name, {
next: "state_start",
text: $([
"It seems this number is not on WhatsApp and we don't offer SMS at this moment.",
"",
"Please register the new number on WhatsApp for the MomConnect Service."
].join("\n"))
});
});


self.states.add("state_exit", function(name) {
return new EndState(name, {
next: "state_start",
Expand All @@ -252,7 +349,8 @@ go.app = function() {
source: "Public USSD",
timestamp: new moment.utc(self.im.config.testing_today).format(),
registered_by: msisdn,
mha: 6
mha: 6,
preferred_channel: self.im.user.answers.preferred_channel
};
return self.rapidpro
.start_flow(self.im.config.flow_uuid, null, "whatsapp:" + _.trim(msisdn, "+"), data)
Expand All @@ -270,7 +368,7 @@ go.app = function() {
});

self.states.add("state_registration_complete", function(name) {
var msisdn = _.get(self.im.user.answers, "state_enter_msisdn", self.im.user.addr);
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
msisdn = utils.readable_msisdn(msisdn, "27");
var whatsapp_message = $(
"You're done! This number {{ msisdn }} will get helpful messages from MomConnect. For " +
Expand Down
Loading