Skip to content

Instantly share code, notes, and snippets.

@leraux
Last active July 19, 2024 16:29
Show Gist options
  • Save leraux/c7a90c624a3629cf03228a433e9cdf68 to your computer and use it in GitHub Desktop.
Save leraux/c7a90c624a3629cf03228a433e9cdf68 to your computer and use it in GitHub Desktop.
var BASE_URL = "http://127.0.0.1:15001/mame-rest-api/";
var DRAFT_URL = BASE_URL + "draft";
$("#button-draft").on("click", readEmailBodyAndDraft);
//$("#button-summarize").on("click", feedback);
function draft(emailBody) {
jQuery.ajax({
url: DRAFT_URL,
type: "POST",
data: {
emailFrom: Office.context.mailbox.item.from,
emailTo: getToAddresses(),
emailCC: Office.context.mailbox.item.cc,
emailBCC: Office.context.mailbox.item.bcc,
emailSubject: Office.context.mailbox.item.subject,
emailBody: emailBody
},
//dataType: "json",
//contentType: "application/text; charset=utf-8",
success: function (response, status) {
//console.log(response.data);
setItemBody(response.data);
}
});
}
function readEmailBodyAndDraft() {
Office.context.mailbox.item.body.getAsync(
"text",
{
asyncContext: ""
},
function callback(result) {
draft(result.value);
}
);
}
function getToAddresses() {
var toAddresses = "";
Office.context.mailbox.item.from.getAsync(function(asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
const msgFrom = asyncResult.value;
console.log("Message from: " + msgFrom.displayName + " (" + msgFrom.emailAddress + ")");
toAddresses = toAddresses + ";" + msgFrom.emailAddress;
} else {
console.error(asyncResult.error);
}
});
}
// Inserts data at the current cursor position.
function setItemBody(bodyData) {
// Identify the body type of the mail item.
Office.context.mailbox.item.body.getTypeAsync((asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
return;
}
// Insert data of the appropriate type into the body.
if (asyncResult.value === Office.CoercionType.Html) {
// Insert HTML into the body.
Office.context.mailbox.item.body.setSelectedDataAsync(
bodyData,
{ coercionType: Office.CoercionType.Html, asyncContext: { optionalVariable1: 1, optionalVariable2: 2 } },
(asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
return;
}
/*
Run additional operations appropriate to your scenario and
use the optionalVariable1 and optionalVariable2 values as needed.
*/
}
);
} else {
// Insert plain text into the body.
Office.context.mailbox.item.body.setSelectedDataAsync(
bodyData,
{ coercionType: Office.CoercionType.Text, asyncContext: { optionalVariable1: 1, optionalVariable2: 2 } },
(asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
return;
}
/*
Run additional operations appropriate to your scenario and
use the optionalVariable1 and optionalVariable2 values as needed.
*/
}
);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment