Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use-strict';
- /**
- * The handler function receives context and variables. It returns new variables to be used in your flow.
- *
- * @param {object} context - the context contains environment variables accessible as context.env.varName1
- * @param {object} variables - the variables from your flow (the input parameters) accessible as variables.varName1
- * @returns {object} - return values that will become available in your flow
- * @throws ExecutionError
- */
- const axios = require('axios');
- var format = require('date-fns/format');
- var parseISO = require('date-fns/parseISO');
- function fetchConversation(offset, variables, context, entireConversation) {
- return axios({
- method: 'GET',
- url: `https://conversations.messagebird.com/v1/conversations/${variables.conversationId}/messages?limit=20${offset > 0 ? `&offset=${offset}`: ''}`,
- headers: {
- Authorization: 'AccessKey ' + context.env.TOKEN,
- },
- })
- .then((response) => {
- let newEntireConversation = [...entireConversation, ...response.data.items];
- if (response.data.totalCount > response.data.count + offset) {
- return fetchConversation(offset + 20, variables, context, newEntireConversation);
- }
- return newEntireConversation;
- })
- .catch((error) => {
- throw error.message;
- });
- }
- async function sendConversationToEmail(conversation, conversationId, recipientEmail, context) {
- let conversationThread = 'Here is the conversation summary:\n';
- let conversationAttachments = [];
- conversation.filter(elem => elem.type !== 'event' && elem.type !== 'interactive').forEach(elem => {
- let msg = `Date: ${format(parseISO(elem.createdDatetime), "PPpp")}\n`
- if (elem.content.text) msg += elem.content.text;
- else if (elem.content.email) msg += elem.content.email.content.text + `\n`;
- else if (elem.type === 'image') conversationAttachments.push(elem.content.image.url.split("https://messaging.messagebird.com/v1/files/")[1]);
- else if (elem.type === 'file') conversationAttachments.push(elem.content.file.url.split("https://messaging.messagebird.com/v1/files/")[1]);
- conversationThread += msg + '\n-------------------\n';
- })
- const response = await axios({
- method: 'POST',
- url: "https://conversations.messagebird.com/v1/send",
- headers: {
- 'Authorization': 'AccessKey ' + context.env.TOKEN,
- },
- data: {
- "to": recipientEmail,
- "from": context.env.FREEMAIL_CHANNEL_ID,
- "type": "email",
- "content": {
- "email": {
- "to": [{
- "address": recipientEmail
- }],
- "from": {
- "name": "MessageBird",
- "address": "email@REPLACE-WITH-YOUR-INBOX-DOMAIN.inbox.ai"
- },
- "subject": `Forwarded conversation [conversationID:${conversationId}]`,
- "content": {
- "text": conversationThread
- },
- "attachments": conversationAttachments.map(attachment => ({
- id: attachment
- }))
- }
- }
- }
- })
- }
- exports.handler = async function(context, variables) {
- if (!variables.hasOwnProperty('conversationId')) {
- throw 'Conversation Id parameter is required';
- }
- if (!variables.hasOwnProperty('recipientEmail')) {
- throw 'recipientEmail parameter is required';
- }
- let offset = 0;
- try {
- const conversation = await fetchConversation(offset, variables, context, []);
- await sendConversationToEmail(conversation, variables.conversationId, variables.recipientEmail, context);
- return {
- status: 'success'
- }
- } catch (e) {
- throw 'Something went wrong';
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement