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

Send chw welcome message to check sms active #597

Merged
merged 1 commit into from
Sep 14, 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
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function (grunt) {
],
ussd_chw_rapidpro: [
'src/index.js',
'src/hub.js',
'src/rapidpro.js',
'<%= paths.src.app.ussd_chw_rapidpro %>',
'src/init.js'
Expand Down Expand Up @@ -152,6 +153,7 @@ module.exports = function (grunt) {
],
ussd_chw_rapidpro: [
'test/setup.js',
'src/hub.js',
'src/rapidpro.js',
'<%= paths.src.app.ussd_chw_rapidpro %>',
'test/ussd_chw_rapidpro.test.js'
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_ccmdd_wc_address_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
102 changes: 99 additions & 3 deletions go-app-ussd_chw_rapidpro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
var go = {};
go;

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.RapidPro = function() {
var vumigo = require('vumigo_v02');
var url_utils = require('url');
Expand Down Expand Up @@ -99,7 +142,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down Expand Up @@ -141,6 +183,15 @@ 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 @@ -594,7 +645,51 @@ go.app = function() {
);
}
},
next: "state_trigger_rapidpro_flow"
next: "state_send_welcome_template"
});
});

self.add("state_send_welcome_template", function(name, opts) {
var msisdn = utils.normalize_msisdn(
_.get(self.im.user.answers, "state_enter_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"))
});
});

Expand Down Expand Up @@ -626,7 +721,8 @@ go.app = function() {
"YYYYMMDD"
).format(),
passport_origin: self.im.user.answers.state_passport_country,
passport_number: self.im.user.answers.state_passport_no
passport_number: self.im.user.answers.state_passport_no,
preferred_channel: self.im.user.answers.preferred_channel
};
return self.rapidpro
.start_flow(self.im.config.flow_uuid, null, "whatsapp:" + _.trim(msisdn, "+"), data)
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_clinic_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_higherhealth_healthcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_mcgcc_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_mqr_faqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_nurse_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_optout_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_pmtct_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_popi_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_public_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
1 change: 0 additions & 1 deletion go-app-ussd_tb_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ go.RapidPro = function() {

self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";

return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
Expand Down
58 changes: 56 additions & 2 deletions src/ussd_chw_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ 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 @@ -476,7 +485,51 @@ go.app = function() {
);
}
},
next: "state_trigger_rapidpro_flow"
next: "state_send_welcome_template"
});
});

self.add("state_send_welcome_template", function(name, opts) {
var msisdn = utils.normalize_msisdn(
_.get(self.im.user.answers, "state_enter_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"))
});
});

Expand Down Expand Up @@ -508,7 +561,8 @@ go.app = function() {
"YYYYMMDD"
).format(),
passport_origin: self.im.user.answers.state_passport_country,
passport_number: self.im.user.answers.state_passport_no
passport_number: self.im.user.answers.state_passport_no,
preferred_channel: self.im.user.answers.preferred_channel
};
return self.rapidpro
.start_flow(self.im.config.flow_uuid, null, "whatsapp:" + _.trim(msisdn, "+"), data)
Expand Down
Loading