Skip to content

Commit

Permalink
Merge pull request #633 from praekeltfoundation/sigma-1155-CAPI-regis…
Browse files Browse the repository at this point in the history
…tration-USSD

Sigma 1155 capi registration ussd
  • Loading branch information
Buhle79 authored Jul 18, 2024
2 parents 4783582 + 0c3b89a commit 9c9f331
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 135 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = function (grunt) {
],
ussd_public_rapidpro: [
'src/index.js',
'src/hub.js',
'src/engage.js',
'src/rapidpro.js',
'<%= paths.src.app.ussd_public_rapidpro %>',
Expand Down Expand Up @@ -131,6 +132,7 @@ module.exports = function (grunt) {
],
ussd_public_rapidpro: [
'test/setup.js',
'src/hub.js',
'src/engage.js',
'src/rapidpro.js',
'<%= paths.src.app.ussd_public_rapidpro %>',
Expand Down
61 changes: 58 additions & 3 deletions go-app-ussd_chw_rapidpro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
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,
"save_status_record": true
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data;

});
};

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;
}
);
};

self.get_whatsapp_template_status = function(status_id) {
var api_url = url.resolve(self.base_url, "/api/v2/whatsapptemplatesendstatus/" + status_id + "/");

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

});
return Hub;
}();

go.RapidPro = function() {
var vumigo = require('vumigo_v02');
var url_utils = require('url');
Expand Down Expand Up @@ -612,9 +667,9 @@ go.app = function() {
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") {
.then(function(data) {
self.im.user.set_answer("preferred_channel", data.preferred_channel);
if (data.preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
Expand Down
50 changes: 43 additions & 7 deletions go-app-ussd_clinic_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ go.Hub = function() {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
"template_name": template_name,
"save_status_record": true
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;
return response.data;

});
};
Expand All @@ -40,6 +41,17 @@ go.Hub = function() {
);
};

self.get_whatsapp_template_status = function(status_id) {
var api_url = url.resolve(self.base_url, "/api/v2/whatsapptemplatesendstatus/" + status_id + "/");

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

});
return Hub;
}();
Expand Down Expand Up @@ -1248,9 +1260,9 @@ go.app = function() {
};
return self.hub
.send_whatsapp_template_message(msisdn, template_name, media)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
.then(function(data) {
self.im.user.set_answer("preferred_channel", data.preferred_channel);
if (data.preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
Expand Down Expand Up @@ -1365,7 +1377,7 @@ go.app = function() {
"Enter the number that matches your answer."
].join("\n")),
choices: [
new Choice("state_trigger_rapidpro_flow", $("Accept")),
new Choice("state_get_whatsapp_template_status", $("Accept")),
new Choice("state_accept_popi_confirm", $("Exit"))
],
});
Expand Down Expand Up @@ -1406,6 +1418,29 @@ go.app = function() {
});
});

self.add("state_get_whatsapp_template_status", function(name, opts) {
var status_id = self.im.user.answers.status_id;

return self.hub
.get_whatsapp_template_status(status_id)
.then(function(data) {
self.im.user.set_answer("preferred_channel", data.preferred_channel);
self.im.user.set_answer("status", data.status);

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.add("state_trigger_rapidpro_flow", function(name, opts) {
var msisdn = utils.normalize_msisdn(
_.get(self.im.user.answers, "state_enter_msisdn", self.im.user.addr), "ZA");
Expand All @@ -1422,7 +1457,8 @@ go.app = function() {
} [self.im.user.answers.state_id_type],
clinic_code: self.im.user.answers.state_clinic_code,
swt: self.im.user.answers.preferred_channel == "SMS" ? "1" : "7",
preferred_channel: self.im.user.answers.preferred_channel
preferred_channel: self.im.user.answers.preferred_channel,
status_id: self.im.user.answers.status_id,
};
var flow_uuid;

Expand Down
22 changes: 17 additions & 5 deletions go-app-ussd_popi_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ go.Hub = function() {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
"template_name": template_name,
"save_status_record": true
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;
return response.data;

});
};
Expand All @@ -40,6 +41,17 @@ go.Hub = function() {
);
};

self.get_whatsapp_template_status = function(status_id) {
var api_url = url.resolve(self.base_url, "/api/v2/whatsapptemplatesendstatus/" + status_id + "/");

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

});
return Hub;
}();
Expand Down Expand Up @@ -1363,9 +1375,9 @@ go.app = function() {

return self.hub
.send_whatsapp_template_message(msisdn, template_name, media)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
.then(function(data) {
self.im.user.set_answer("preferred_channel", data.preferred_channel);
if (data.preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
Expand Down
104 changes: 58 additions & 46 deletions go-app-ussd_public_rapidpro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
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,
"save_status_record": true
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data;

});
};

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;
}
);
};

self.get_whatsapp_template_status = function(status_id) {
var api_url = url.resolve(self.base_url, "/api/v2/whatsapptemplatesendstatus/" + status_id + "/");

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

});
return Hub;
}();

go.Engage = function() {
var vumigo = require('vumigo_v02');
var events = vumigo.events;
Expand Down Expand Up @@ -162,49 +217,6 @@ 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 Down Expand Up @@ -457,9 +469,9 @@ go.app = function() {
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") {
.then(function(data) {
self.im.user.set_answer("preferred_channel", data.preferred_channel);
if (data.preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
Expand Down
16 changes: 14 additions & 2 deletions src/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ go.Hub = function() {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
"template_name": template_name,
"save_status_record": true
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;
return response.data;

});
};
Expand All @@ -37,6 +38,17 @@ go.Hub = function() {
);
};

self.get_whatsapp_template_status = function(status_id) {
var api_url = url.resolve(self.base_url, "/api/v2/whatsapptemplatesendstatus/" + status_id + "/");

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

});
return Hub;
}();
Loading

0 comments on commit 9c9f331

Please sign in to comment.