如何通过电子邮件回复聊天对话

时间:2017-10-24 09:56:50

标签: angularjs meteor sendmail mailgun angular-meteor

我必须创建一个功能,当任何用户在聊天室内进行任何对话时,会向其他聊天用户生成电子邮件。其他用户可以查看电子邮件,并且可以从同一电子邮件回复聊天对话。我正在使用角流星。我该如何使用此功能? sendgrid或mailgun中是否有任何API来处理来自电子邮件的呼叫并添加对话?或者我必须创建POST / GET方法以接受在邮件中点击按钮的呼叫并保存回复的文本?

1 个答案:

答案 0 :(得分:1)

您可以告诉sendgrid在收到收到的电子邮件时对您的服务器进行REST api调用。

发送电子邮件时,请将回复电子邮件设置为@ chat-reply.myserver.com

然后在服务器代码中设置一个终点来处理这些请求。您的代码需要从传入地址查找对话,然后才能在聊天中保存记录。

这是一些代码......

import { Meteor } from 'meteor/meteor'
formidable = require('formidable');     // Formidable does upload form/file parsing
import { Profiles } from '../imports/api/collections';
import { inboundReply } from '../imports/api/inbound/methods.js';

const debug = require('debug')('myapp:inbound')

// Needs to run on the server and client, why this is not in the routing.js file
//   which is only only runs on the client.
//   Inbound emails, for loop reply
//
// This is a RESTAPI end point which is called by sendgrid,
//   any email to xxxx@chat-reply.myserver.com.au will come here. Our job
//   is to parse it , work out which loop it relates to, and save it as a message
//   in the database
//
Router.route('/inbound', function () {

    // Need to use formidable because SendGrid inbound data is encoded as a multipart/form-data
    const form = new formidable.IncomingForm();
    // Meteor bind eviron. to get callback
    debug(this.request.body)
    let r = this.response
    form.parse(this.request, Meteor.bindEnvironment(function (error, fields, files) {
      if (error)
        console.error(error);
      let errs = []

      // Gets the to field
      const toField = _.find(fields, function(value, key) { if (key === 'to') { return value; }});

      // Gets the from field
      const fromField = _.find(fields, function(value, key) { if (key === 'from') { return value; }});

      // Gets the html content, email
      const content = _.find(fields, function(value, key) { if (key === 'text') { return value; }});

      let cleanContent;
      if (content){
        // Logger.trace({content: content});
        // Regex removes html
        // cleanContent  = content.replace(/<br>/ig, "\n");
        // const regex = /(<([^>]+)>)/ig
        // cleanContent  = cleanContent.replace(regex, "");
        // Logger.trace({cleanContent: cleanContent});
        let lines = content.split(/\n/);
        debug("Incoming body",lines);