Skip to content

Commit

Permalink
Merge pull request #123 from AdarshNaidu/unread-messages-intent
Browse files Browse the repository at this point in the history
[Update] Unread messages intent
  • Loading branch information
Sing-Li authored Aug 25, 2020
2 parents 5e3c94f + e27a8d8 commit 214381f
Show file tree
Hide file tree
Showing 17 changed files with 1,353 additions and 176 deletions.
10 changes: 10 additions & 0 deletions lambda/custom/apiEndpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ module.exports = {
groupcounterurl: `${ serverurl }/api/v1/groups.counters?roomId=`,
createimurl: `${ serverurl }/api/v1/im.create`,
getsubscriptionsurl: `${ serverurl }/api/v1/subscriptions.get`,
setannouncementchannelurl: `${ serverurl }/api/v1/channels.setAnnouncement`,
setannouncementgroupurl: `${ serverurl }/api/v1/groups.setAnnouncement`,
setdescriptionchannelurl: `${ serverurl }/api/v1/channels.setDescription`,
setdescriptiongroupurl: `${ serverurl }/api/v1/groups.setDescription`,
settopicchannelurl: `${ serverurl }/api/v1/channels.setTopic`,
settopicgroupurl: `${ serverurl }/api/v1/groups.setTopic`,
renamechannelurl: `${ serverurl }/api/v1/channels.rename`,
renamegroupurl: `${ serverurl }/api/v1/groups.rename`,
addleadertochannelurl: `${ serverurl }/api/v1/channels.addLeader`,
addleadertogroupurl: `${ serverurl }/api/v1/groups.addLeader`,
addownertochannelurl: `${ serverurl }/api/v1/channels.addOwner`,
Expand All @@ -53,4 +61,6 @@ module.exports = {
kickuserfromchannelurl: `${ serverurl }/api/v1/channels.kick`,
kickuserfromgroupurl: `${ serverurl }/api/v1/groups.kick`,
setstatusurl: `${ serverurl }/api/v1/users.setStatus`,
readpinnedmessagesurl: `${ serverurl }/api/v1/chat.getPinnedMessages`,
getmentionedmessagesurl: `${ serverurl }/api/v1/chat.getMentionedMessages`,
};
33 changes: 33 additions & 0 deletions lambda/custom/handlers/Channels/GetMentionsIntentHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { ri } = require('@jargon/alexa-skill-sdk');

const helperFunctions = require('../../helperFunctions');

const GetMentionsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'GetMentionsIntent';
},
async handle(handlerInput) {
try {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;

const headers = await helperFunctions.login(accessToken);
const speechText = await helperFunctions.getAllUnreadMentions(headers);
const repromptText = ri('GENERIC_REPROMPT');

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.getResponse();
} catch (error) {
console.error(error);
}
},
};

module.exports = {
GetMentionsIntentHandler,
};
34 changes: 28 additions & 6 deletions lambda/custom/handlers/Channels/GetUnreadMessagesIntentHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { ri } = require('@jargon/alexa-skill-sdk');
const { replaceWhitespacesFunc, login, getUnreadCounter, channelUnreadMessages } = require('../../helperFunctions');
const { resolveChannelname, login, getUnreadCounter, channelUnreadMessages, getGroupUnreadCounter, groupUnreadMessages } = require('../../helperFunctions');
const { supportsAPL } = require('../../utils');
const titleMessageBoxTemplate = require('../../APL/templates/titleMessageBoxTemplate');


const GetUnreadMessagesIntentHandler = {
Expand All @@ -12,19 +14,39 @@ const GetUnreadMessagesIntentHandler = {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;
const channelNameData = handlerInput.requestEnvelope.request.intent.slots.readunreadschannel.value;
const channelName = replaceWhitespacesFunc(channelNameData);

const headers = await login(accessToken);
const unreadCount = await getUnreadCounter(channelName, headers);
const speechText = await channelUnreadMessages(channelName, unreadCount, headers);
const response = await resolveChannelname(handlerInput.requestEnvelope.request.intent.slots.readunreadschannel.value, headers, true);
const room = response[0];
let speechText;
if (room.type === 'c') {
const unreadCount = await getUnreadCounter(room.name, headers);
speechText = await channelUnreadMessages(room.name, unreadCount, headers);
} else {
const unreadCount = await getGroupUnreadCounter(room.id, headers);
speechText = await groupUnreadMessages(room.name, room.id, unreadCount, headers);
}

const repromptText = ri('GENERIC_REPROMPT');

if (supportsAPL(handlerInput)) {
const data = {
title: room.name,
message: handlerInput.translate(speechText.key, speechText.params),
};

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.addDirective(titleMessageBoxTemplate(data))
.getResponse();
}

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.withSimpleCard(ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.CARD_TITLE'), speechText)
.getResponse();
} catch (error) {
console.error(error);
Expand Down
33 changes: 33 additions & 0 deletions lambda/custom/handlers/Channels/GetUnreadsIntentHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { ri } = require('@jargon/alexa-skill-sdk');

const helperFunctions = require('../../helperFunctions');

const GetUnreadsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'GetUnreadsIntent';
},
async handle(handlerInput) {
try {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;

const headers = await helperFunctions.login(accessToken);
const speechText = await helperFunctions.getAllUnreads(headers);
const repromptText = ri('GENERIC_REPROMPT');

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.getResponse();
} catch (error) {
console.error(error);
}
},
};

module.exports = {
GetUnreadsIntentHandler,
};
33 changes: 0 additions & 33 deletions lambda/custom/handlers/Channels/PrivateChannelIntents.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,7 @@ const GroupLastMessageIntentHandler = {
},
};

const GetGroupUnreadMessagesIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'ReadGroupUnreadsIntent';
},
async handle(handlerInput) {
try {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;
const channelNameData = handlerInput.requestEnvelope.request.intent.slots.groupunreadschannelname.value;
const channelName = helperFunctions.replaceWhitespacesFunc(channelNameData);

const headers = await helperFunctions.login(accessToken);
const roomid = await helperFunctions.getGroupId(channelName, headers);
const unreadCount = await helperFunctions.getGroupUnreadCounter(roomid, headers);
const speechText = await helperFunctions.groupUnreadMessages(channelName, roomid, unreadCount, headers);
const repromptText = ri('GENERIC_REPROMPT');


return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.withSimpleCard(ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.CARD_TITLE'), speechText)
.getResponse();
} catch (error) {
console.error(error);
}
},
};

module.exports = {
PostGroupEmojiMessageIntentHandler,
GroupLastMessageIntentHandler,
GetGroupUnreadMessagesIntentHandler,
};
46 changes: 46 additions & 0 deletions lambda/custom/handlers/Channels/ReadMentionsIntentHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { ri } = require('@jargon/alexa-skill-sdk');

const helperFunctions = require('../../helperFunctions');

const ReadMentionsIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'ReadMentionsIntent';
},
async handle(handlerInput) {
try {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;

const headers = await helperFunctions.login(accessToken);

const channelName = handlerInput.requestEnvelope.request.intent.slots.channelname.value;
const bestMatchingChannel = await helperFunctions.resolveChannelname(channelName, headers, true);
const channel = bestMatchingChannel[0];

let count;
if (channel.type === 'c') {
count = await helperFunctions.getUnreadMentionsCountChannel(channel.name, headers);
} else {
count = await helperFunctions.getUnreadMentionsCountGroup(channel.id, headers);
}

const speechText = await helperFunctions.readUnreadMentions(channel.id, channel.name, count, headers);
const repromptText = ri('GENERIC_REPROMPT');

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.getResponse();

} catch (error) {
console.error(error);
}
},
};

module.exports = {
ReadMentionsIntentHandler,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const { ri } = require('@jargon/alexa-skill-sdk');
const { login, resolveChannelname, readPinnedMessages } = require('../../helperFunctions');
const titleMessageBoxTemplate = require('../../APL/templates/titleMessageBoxTemplate');
const { supportsAPL } = require('../../utils');

const ReadPinnedMessagesIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'ReadPinnedMessagesIntent'
&& handlerInput.requestEnvelope.request.dialogState === 'COMPLETED';
},
async handle(handlerInput) {
try {
const {
accessToken,
} = handlerInput.requestEnvelope.context.System.user;

const headers = await login(accessToken);
const resolvedChannelDetails = await resolveChannelname(handlerInput.requestEnvelope.request.intent.slots.channelname.value, headers, true);

const pinnedMessages = await readPinnedMessages(resolvedChannelDetails[0].id, resolvedChannelDetails[0].name, headers);

// if there were any pinned messages helper function would return an array, otherwise there are no pinned messages
if (!Array.isArray(pinnedMessages)) {
const repromptText = ri('GENERIC_REPROMPT');
if (supportsAPL(handlerInput)) {
const data = {
title: '',
message: handlerInput.translate(pinnedMessages.key, pinnedMessages.params),
};

return handlerInput.jrb
.speak(pinnedMessages)
.speak(repromptText)
.reprompt(repromptText)
.addDirective(titleMessageBoxTemplate(data))
.getResponse();
}

return handlerInput.jrb
.speak(pinnedMessages)
.speak(repromptText)
.reprompt(repromptText)
.getResponse();

}

const no_of_pinned_messages = pinnedMessages.length;

const speechText = ri('PINNED_MESSAGES.SUCCESS', { no_of_pinned_messages, channelname: resolvedChannelDetails[0].name, username: pinnedMessages[no_of_pinned_messages - 1][0], message: pinnedMessages[no_of_pinned_messages - 1][1] });
const repromptText = ri('GENERIC_REPROMPT');


if (!supportsAPL(handlerInput)) {
return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.getResponse();
}


let apltext = '';

for (const message of pinnedMessages) {
apltext += `<b>${ message[0] }:</b>${ message[1] }<br><br>`;
}

const data = {
title: `Pinned Messages in ${ resolvedChannelDetails[0].name }: ${ pinnedMessages.length }`,
message: apltext,
};

return handlerInput.jrb
.speak(speechText)
.speak(repromptText)
.reprompt(repromptText)
.addDirective(titleMessageBoxTemplate(data))
.getResponse();

} catch (error) {
console.error(error);
}
},
};

module.exports = {
ReadPinnedMessagesIntentHandler,
};
Loading

0 comments on commit 214381f

Please sign in to comment.