如何在没有Gmail API原始消息的情况下获取回复消息

如何在没有Gmail API原始消息的情况下获取回复消息,gmail,gmail-api,Gmail,Gmail Api,我想在没有原始消息的线程中获得回复消息。但是,当我使用Users.messages:GET或Users.threads:GET时,我也会收到回复(根据需要)和原始消息(不需要)。请参见下面的代码截图 (据我所知,这个问题也被提出了,但是我没有发现提议的解决方案回答了这个问题,提议解决方案的海报建议我开始一个新问题。我尝试了用户。Thole建议的线程得到了相同的结果。) 我是一个笨蛋,所以非常感谢所有的帮助,如果我遗漏了一些明显的东西,我道歉 代码 var gapiGETRequest=函数(ga

我想在没有原始消息的线程中获得回复消息。但是,当我使用Users.messages:GET或Users.threads:GET时,我也会收到回复(根据需要)和原始消息(不需要)。请参见下面的代码截图

(据我所知,这个问题也被提出了,但是我没有发现提议的解决方案回答了这个问题,提议解决方案的海报建议我开始一个新问题。我尝试了用户。Thole建议的线程得到了相同的结果。)

我是一个笨蛋,所以非常感谢所有的帮助,如果我遗漏了一些明显的东西,我道歉

代码
var gapiGETRequest=函数(gapiRequestURL)
{
var xmlHttp=new XMLHttpRequest();
open(“GET”,gapiRequestURL,false);
xmlHttp.send(空);
返回xmlHttp.responseText;
}
var gapiRequestInboxMessagesAndToken=”https://www.googleapis.com/gmail/v1/users/me/messages?q=-在%3收件箱和访问令牌=“+thisToken”中标记%3已发送+
var allMessagesReceived=gapiGETRequest(gapiRequestInboxMessagesAndToken)
var allMessagesObject=JSON.parse(allMessagesReceived)
var messageIdsOfReceivedMessages=[];
var getIdsOfReceivedMessages=函数(responseObject){
对于(变量i=0;i
响应
您将收到完整的回复信息。当报告回复时,他们引用了原始消息,而原始消息的文本在回复消息中。你可能只想做Gmail和许多其他现代电子邮件应用程序所做的事情,折叠/隐藏任何以>开头的回复文本。这是我的解决方案。虽然有点长,但我试着尽可能详细地记录下来

处理Gmail API返回的
消息

输入:

Hello. This is my reply to message.

On Thu, Apr 30, 2020 at 8:29 PM John Doe <john.doe@example.com>
wrote:

> Hey. This is my message.
>


-- 
John Doe
My Awesome Signature
Hello. This is my reply to message.
代码:(不幸的是,它没有语法突出显示:p)

const message=wait getMessageFromGmailApi();
const text=getGoogleMessageText(消息);
console.log(text,'-1)| |(!!toEmail&&text.indexOf(toEmailWithArrows)>-1);
如果(isEmailWithHistory){
//注意:带有箭头的第一封历史电子邮件
const historyEmailWithArrows=this.findFirstString(从EmailWithArrows到EmailWithArrows,文本);
//注意:完成后请移除所有内容``
text=text.substring(0,text.indexOf(historyEmailWithArrows)+historyEmailWithArrows.length);
//注意:删除包含``
const fromRegExp=new RegExp(`^.*${historyEmailWithArrows}.*$`,'mg');
text=text.replace(fromRegExp',);
}
text=text.trim()
返回文本;
}
函数getGoogleMessageEmailFromHeader(headerName,message){
const header=message.payload.headers.find((header)=>header.name==headerName);
如果(!标题){
返回null;
}
const headerValue=header.value;//John Doe
const email=headerValue.substring(
headerValue.lastIndexOf(“”)
);
返回电子邮件;//约翰。doe@example.com
}
函数findFirstString(a、b、str){
if(str.indexOf(a)=-1)返回b;
if(str.indexOf(b)=-1)返回a;
返回(str.indexOf(a)
AFAIK,您不能,因为它是回复电子邮件正文内容的一部分。Gmail只是发送回复邮件中的任何文本。您必须自己解析该文本才能删除不需要的内容。PHP部分中缺少
preg_replace
行:
var decodedMessageContents=atob(encodedMessageContents.replace(/-/g,“+”).replace(//g,“/”)谢谢发布代码!“>”是不够的,因为在回答上面还有一行,上面写着“年,约翰·多伊写道:…”
Hello. This is my reply to message.
const message = await getMessageFromGmailApi();

const text = getGoogleMessageText(message);

console.log(text, '<= AWESOME RESULT');


function getGoogleMessageText(message) {
    let text = '';

    const fromEmail = getGoogleMessageEmailFromHeader('From', message);
    const toEmail = getGoogleMessageEmailFromHeader('To', message);

    let part;
    if (message.payload.parts) {
        part = message.payload.parts.find((part) => part.mimeType === 'text/plain');
    }

    let encodedText;
    if (message.payload.parts && part && part.body.data) {
        encodedText = part.body.data;
    } else if (message.payload.body.data) {
        encodedText = message.payload.body.data;
    }

    if (encodedText) {
        const buff = new Buffer(encodedText, 'base64');
        text = buff.toString('ascii');
    }

    // NOTE: We need to remove history of email.
    // History starts with line (example): 'On Thu, Apr 30, 2020 at 8:29 PM John Doe <john.doe@example.com> wrote:'
    //
    // We also don't know who wrote the last message in history, so we use the email that
    // we meet first: 'fromEmail' and 'toEmail'
    const fromEmailWithArrows = `<${fromEmail}>`;
    const toEmailWithArrows = `<${toEmail}>`;
    // NOTE: Check if email has history
    const isEmailWithHistory = (!!fromEmail && text.indexOf(fromEmailWithArrows) > -1) || (!!toEmail && text.indexOf(toEmailWithArrows) > -1);

    if (isEmailWithHistory) {
       // NOTE: First history email with arrows
       const historyEmailWithArrows = this.findFirstSubstring(fromEmailWithArrows, toEmailWithArrows, text);

       // NOTE: Remove everything after `<${fromEmail}>`
       text = text.substring(0, text.indexOf(historyEmailWithArrows) + historyEmailWithArrows.length);
       // NOTE: Remove line that contains `<${fromEmail}>`
       const fromRegExp = new RegExp(`^.*${historyEmailWithArrows}.*$`, 'mg');
       text = text.replace(fromRegExp, '');
    }

    text = text.trim()

    return text;
}


function getGoogleMessageEmailFromHeader(headerName, message) {
    const header = message.payload.headers.find((header) => header.name === headerName);

    if (!header) {
        return null;
    }

    const headerValue = header.value; // John Doe <john.doe@example.com>

    const email = headerValue.substring(
        headerValue.lastIndexOf('<') + 1,
        headerValue.lastIndexOf('>')
    );

    return email; // john.doe@example.com
}


function findFirstSubstring(a, b, str) {
    if (str.indexOf(a) === -1) return b;
    if (str.indexOf(b) === -1) return a;

    return (str.indexOf(a) < str.indexOf(b))
        ? a
        : b; // NOTE: (str.indexOf(b) < str.indexOf(a))
}