Skip to content

Commit

Permalink
Added APLT Document for CharacterDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
ijaz26 committed Dec 14, 2024
1 parent 4142c42 commit 9032b05
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
15 changes: 15 additions & 0 deletions lambda/aplDocuments/characterDisplayApl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "APLT",
"version": "1.0",
"mainTemplate": {
"parameters": [
"payload"
],
"items": [
{
"type": "Text",
"text": "${payload.data.text}"
}
]
}
}
1 change: 0 additions & 1 deletion lambda/handlers/apiHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const axios = require("axios");
const { lang } = require("moment");
const mawaqitBaseUrl = process.env.baseUrl;

const getMosqueList = async (
Expand Down
8 changes: 6 additions & 2 deletions lambda/handlers/intentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const NextPrayerTimeIntentHandler = {
// Find the first non-null Jumu'ah time
const firstNonNullJumua = jumuaTimes.filter((time) => time !== null && time !== undefined).join(", ");
if (firstNonNullJumua) {
helperFunctions.checkForCharacterDisplay(handlerInput, firstNonNullJumua);
return handlerInput.responseBuilder
.speak(
requestAttributes.t(
Expand All @@ -175,6 +176,7 @@ const NextPrayerTimeIntentHandler = {
// Find the first non-null Eid time
const firstNonNullEid = eidTimes.filter((time) => time !== null && time !== undefined).join(", ");
if (firstNonNullEid) {
helperFunctions.checkForCharacterDisplay(handlerInput, firstNonNullEid);
return handlerInput.responseBuilder
.speak(
requestAttributes.t(
Expand Down Expand Up @@ -229,6 +231,7 @@ const NextPrayerTimeIntentWithoutNameHandler = {
return await helperFunctions.checkForPersistenceData(handlerInput);
}
const prayerTimeDetails = helperFunctions.getNextPrayerTime(requestAttributes, mosqueTimes.times, await helperFunctions.getUserTimezone(handlerInput), requestAttributes.t("prayerNames"));
helperFunctions.checkForCharacterDisplay(handlerInput, prayerTimeDetails.time);
const speakOutput = requestAttributes.t("nextPrayerWithoutMosquePrompt", prayerTimeDetails.name, prayerTimeDetails.time, prayerTimeDetails.diffInMinutes) + requestAttributes.t("doYouNeedAnythingElsePrompt");
return handlerInput.responseBuilder
.speak(speakOutput)
Expand Down Expand Up @@ -281,6 +284,7 @@ const NextIqamaTimeIntentHandler = {
iqamaTimes
);
console.log("Next Iqama Time: ", nextIqamaTime);
helperFunctions.checkForCharacterDisplay(handlerInput, nextIqamaTime.diffInMinutes);
return handlerInput.responseBuilder
.speak(
requestAttributes.t(
Expand Down Expand Up @@ -692,10 +696,10 @@ const HadithIntentHandler = {
console.log("In HadithIntentHandler");
const requestAttributes = handlerInput.attributesManager.getRequestAttributes();
const locale = helperFunctions.splitLanguage(Alexa.getLocale(handlerInput.requestEnvelope));
const hadith = await getRandomHadith(locale).catch((error) => {{
const hadith = await getRandomHadith(locale).catch((error) => {
console.log("Error in fetching hadith: ", error);
return requestAttributes.t("hadithErrorPrompt");
}});
});

return handlerInput.responseBuilder
.speak(hadith)
Expand Down
31 changes: 26 additions & 5 deletions lambda/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mosqueListApl = require("./aplDocuments/mosqueListApl.json");
const moment = require("moment-timezone");
const { getLatLng } = require("./handlers/googleGeoApiHandler.js");
const { translate, detectLanguage } = require('./handlers/googleTranslateHandler.js');
const prayerTimeApl = require("./aplDocuments/characterDisplayApl.json");

const getPersistedData = async (handlerInput) => {
try {
Expand All @@ -28,9 +29,9 @@ const checkForConsentTokenToAccessDeviceLocation = (handlerInput) => {
);
};

const createDirectivePayload = (aplDocument, dataSources = {}) => {
const createDirectivePayload = (aplDocument, dataSources = {}, type = "Alexa.Presentation.APL.RenderDocument") => {
return {
type: "Alexa.Presentation.APL.RenderDocument",
type: type,
token: uuidv4(),
document: aplDocument,
datasources: dataSources,
Expand Down Expand Up @@ -92,7 +93,7 @@ const getPrayerTimingsForMosque = async (
mosqueTimes,
speakOutput
) => {
const { responseBuilder, attributesManager } = handlerInput;
const { attributesManager } = handlerInput;
const requestAttributes = attributesManager.getRequestAttributes();
const persistedData =
attributesManager.getSessionAttributes().persistentAttributes;
Expand All @@ -112,7 +113,8 @@ const getPrayerTimingsForMosque = async (
nextPrayerTime.time,
nextPrayerTime.diffInMinutes
);
return responseBuilder.speak(speakOutput).withShouldEndSession(false).getResponse();
checkForCharacterDisplay(handlerInput, nextPrayerTime.time);
return handlerInput.responseBuilder.speak(speakOutput).withShouldEndSession(false).getResponse();
} catch (error) {
console.log("Error in fetching prayer timings: ", error);
if (error === "Mosque not found") {
Expand Down Expand Up @@ -294,6 +296,14 @@ const getUserTimezone = async (handlerInput) => {
return userTimeZone;
};

function checkForCharacterDisplay(handlerInput, nextPrayerTime) {
if (Alexa.getSupportedInterfaces(handlerInput.requestEnvelope)["Alexa.Presentation.APLT"]) {
const dataSource = createDataSourceForPrayerTiming(nextPrayerTime);
const aplDirective = createDirectivePayload(prayerTimeApl, dataSource, "Alexa.Presentation.APLT.RenderDocument");
handlerInput.responseBuilder.addDirective(aplDirective);
}
}

function calculateMinutes(requestAttributes, start, end) {
// Create Date objects from the input strings
const startDate = new Date(start);
Expand Down Expand Up @@ -405,6 +415,7 @@ const getPrayerTimeForSpecificPrayer = (
minutesDiff
);
}
checkForCharacterDisplay(handlerInput, prayerTime);
return handlerInput.responseBuilder
.speak(
requestAttributes.t(
Expand Down Expand Up @@ -492,6 +503,14 @@ const splitLanguage = (locale) => {
return locale.split("-")[0];
}

const createDataSourceForPrayerTiming = (time) => {
return {
"data": {
"text": time
}
}
}

module.exports = {
getPersistedData,
checkForConsentTokenToAccessDeviceLocation,
Expand All @@ -510,5 +529,7 @@ module.exports = {
generateNextPrayerTime,
translateText,
callDirectiveService,
splitLanguage
splitLanguage,
createDataSourceForPrayerTiming,
checkForCharacterDisplay
};
4 changes: 1 addition & 3 deletions lambda/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const LocalizationInterceptor = {
const requestType = Alexa.getRequestType(handlerInput.requestEnvelope);
console.log("Request Type: ", requestType);
if(isValidRequestType(requestType)){
//should not localize the response when the skill is disabled
return;
}
let locale = Alexa.getLocale(handlerInput.requestEnvelope);
Expand Down Expand Up @@ -135,7 +134,6 @@ const SavePersistenceAttributesToSession = {
const requestType = Alexa.getRequestType(handlerInput.requestEnvelope);
console.log("Request Type: ", requestType);
if(isValidRequestType(requestType)){
//should not localize the response when the skill is disabled
return;
}
const isNewSession = Alexa.isNewSession(handlerInput.requestEnvelope);
Expand Down Expand Up @@ -177,7 +175,6 @@ const SetApiKeysAsEnvironmentVariableFromAwsSsm = {
const requestType = Alexa.getRequestType(handlerInput.requestEnvelope);
console.log("Request Type: ", requestType);
if(isValidRequestType(requestType)){
//should not localize the response when the skill is disabled
return;
}
const isNewSession = Alexa.isNewSession(handlerInput.requestEnvelope);
Expand All @@ -188,6 +185,7 @@ const SetApiKeysAsEnvironmentVariableFromAwsSsm = {
};

function isValidRequestType(requestType) {
//skip processing for skill disabled events and audio player requests
return requestType === "AlexaSkillEvent.SkillDisabled" || requestType.startsWith("AudioPlayer.") || requestType.startsWith("PlaybackController");
}

Expand Down
2 changes: 1 addition & 1 deletion lambda/prompts/fr_FR.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ module.exports = {
adhanReciterSuccessPrompt: `Parfait, %s est maintenant votre récitation favorite pour l'adhan ! `,
titleForAdhaanReciterList: `Récitateurs d'Adhan`,
chooseAdhaanByTouchPrompt: "ou vous pouvez aussi cliquer sur la récitation de votre choix sur votre écran.",
hadithErrorPrompt: `Sorry, I couldn't find any hadiths. Please try again.`,
hadithErrorPrompt: `Désolé, je n'ai trouvé aucun hadith. Veuillez réessayer.`,
},
};

0 comments on commit 9032b05

Please sign in to comment.