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

chore: remove previous code #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
270 changes: 24 additions & 246 deletions actions.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,12 @@
"use strict";

const { rescheduleDeliveryDate, updateStatus } = require("./api");
const {
sendLocationBasedCard,
sendNeedDisasterCard,
unsubscribeUser,
sendDisasterCard,
} = require("./lib");
const { getCardLink } = require("./api");

let SUCCESS = { statusCode: "200", body: "Success" };
let BAD_REQ = { statusCode: "400" };

async function replyWithLocationBasedCard(params, context) {
try {
const { whatsapp, phone, language } = params;
await sendLocationBasedCard(whatsapp, phone, "region", language);
context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}

async function replyForNeedDisasterCard(params, context) {
try {
const { whatsapp, phone, language } = params;
await sendNeedDisasterCard(whatsapp, phone, "need", language);
context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}

async function replyForUnsubscribeUser(params, context) {
try {
const { whatsapp, phone, language } = params;
await unsubscribeUser(whatsapp, phone, language);

context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}

async function replyOnUpdateStatus(params, context) {
try {
const { whatsapp, phone, messageText } = params;
const splitCode = messageText.split("-");
const extractNeedIds = splitCode.slice(1);
await updateStatus(phone, messageText, extractNeedIds);

await whatsapp.sendTemplate({
recipientPhone: phone,
template: "crowdlogistics_donor_acknowledgment",
languageCode: data[0]["giver_language"] || "id",
});

context.succeed(SUCCESS);
} catch (error) {
await params.whatsapp.sendText({
recipientPhone: phone,
message: "Invalid code , Please enter the correct code",
});
context.succeed({
statusCode: "200",
body: err,
});
} finally {
return;
}
}

const DISASTERS = {
flood: {
en: {
title: "Flood",
payload: "flood",
},
ur: {
title: "سیلاب",
payload: "سیلاب",
},
},
};

const HEADER_RADIO_BUTTONS = {
en: "Disaster Bot",
ur: "ڈیزاسٹر بوٹ",
};

const MENU_TITLE = {
en: "Select disaster type",
ur: "تباہی کی قسم منتخب کریں۔",
};

const BODY_TEXT = {
en: "Hi! I’m Disaster Bot! Select the disaster you would like to report from the list",
ur: "ہائے! میں ڈیزاسٹر بوٹ ہوں! فہرست سے وہ آفت منتخب کریں جس کی آپ اطلاع دینا چاہتے ہیں۔",
};

function constructMenuPayload(language) {
return {
headerText: HEADER_RADIO_BUTTONS[language],
actionTitle: MENU_TITLE[language],
bodyText: BODY_TEXT[language],
footerText: "Approved by www.aafatinfo.pk",
listOfSections: [
{
title: "",
rows: [
{
title: DISASTERS["flood"][language]["title"],
id: DISASTERS["flood"][language]["payload"],
},
],
},
],
};
}

async function replyWithMenu(params, context) {
try {
const { whatsapp, phone, messageText } = params;
let menuPayload;

switch (messageText) {
case "سیلاب":
menuPayload = constructMenuPayload("ur");
break;
case "report":
menuPayload = constructMenuPayload("en");
break;
default:
return;
}

await whatsapp.sendRadioButtons({
recipientPhone: phone,
...menuPayload,
});

context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}

const DEFAULT_LANGUAGE = "en";

const GREET = {
en: 'Hi! I`m Disaster Bot! Click "Report" to select the disaster you would like to report',
en: "Hi! I am Aafat Bot! Click report to inform others about rain conditions near you. I will reply here once your report is submitted.",
ur: 'ہائے! میں ڈیزاسٹر بوٹ ہوں! آپ جس آفت کی اطلاع دینا چاہتے ہیں اسے منتخب کرنے کے لیے "رپورٹ" پر کلک کریں۔',
};

Expand All @@ -172,11 +22,8 @@ async function replyWithGreeting(params, context) {
await whatsapp.sendSimpleButtons({
message: GREET[DEFAULT_LANGUAGE],
recipientPhone: phone,
footerText: "Approved by www.aafatinfo.pk",
listOfButtons: [
{
title: START["ur"],
id: "report-ur",
},
{
title: START["en"],
id: "report",
Expand All @@ -192,69 +39,35 @@ async function replyWithGreeting(params, context) {
}
}

async function replyForUnsupportedMessageType(params, context) {
async function replyOnReportButtonClicked(params, context) {
const { phone, whatsapp } = params;

try {
const { whatsapp, phone } = params;
await whatsapp.sendText({
recipientPhone: phone,
message: "Unsupported type, Kindly send a text message",
});
const floodCardLink = await getCardLink(
phone,
"whatsapp",
"flood",
"en",
"dev"
);

const cardLink = `https://cards-dev.aafatinfo.pk/${floodCardLink.data.cardId}/flood/location`;
const replyText = `Thank you for your report! View and share your report using the link \n${cardLink}\n#ReduceRiskTogether`;
await whatsapp.sendText({ message: replyText, recipientPhone: phone });
context.succeed(SUCCESS);
} catch (error) {
console.error(error);
} finally {
return;
console.error("Error sending card link: ", error);
throw error;
}
}

async function replyForQuickReplyMessage(params, context) {
const { button, whatsapp } = params;
const payloadSplit = button.payload.split("-");
const needIds = payloadSplit[1].split(",");
const needUserId = payloadSplit[2];
const needLanguage = payloadSplit[3];

if (payloadSplit[0] === "one" || payloadSplit[0] === "two") {
try {
const data = await rescheduleDeliveryDate(payloadSplit[0], needIds);

await whatsapp.sendTemplate({
recipientPhone: needUserId,
template: "crowdlogistics_donor_reschedule",
message: [data[0][0].promised_data],
languageCode: needLanguage,
});

context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}
}

async function replyForSimpleButtonMessage(params, context) {
const { whatsapp, phone, buttonId } = params;
let menuPayload;

switch (buttonId) {
case "report-ur":
menuPayload = constructMenuPayload("ur");
break;
case "report":
menuPayload = constructMenuPayload("en");
break;
default:
return;
}

async function replyForUnsupportedMessageType(params, context) {
try {
await whatsapp.sendRadioButtons({
const { whatsapp, phone } = params;
await whatsapp.sendText({
recipientPhone: phone,
...menuPayload,
message: "Unsupported type, Kindly send a text message",
});

context.succeed(SUCCESS);
} catch (error) {
console.error(error);
Expand All @@ -263,43 +76,8 @@ async function replyForSimpleButtonMessage(params, context) {
}
}

async function replyForRadioButtonMessage(params, context) {
const { selectionId, phone, whatsapp, language } = params;

let lan;
console.log("replyForRadioButtonMessage", selectionId, phone, language);
let disasterType;
switch (selectionId) {
case "flood":
disasterType = "flood";
lan = "en";
break;
case "سیلاب":
disasterType = "flood";
lan = "ur";
break;
}

try {
await sendDisasterCard(whatsapp, phone, disasterType, lan);

context.succeed(SUCCESS);
} catch (error) {
context.succeed({ ...BAD_REQ, body: error });
} finally {
return;
}
}

module.exports = {
replyWithLocationBasedCard,
replyForNeedDisasterCard,
replyForUnsubscribeUser,
replyOnUpdateStatus,
replyWithMenu,
replyWithGreeting,
replyOnReportButtonClicked,
replyForUnsupportedMessageType,
replyForQuickReplyMessage,
replyForSimpleButtonMessage,
replyForRadioButtonMessage,
};
Loading