Tips for responding to a chat conversation via email

Looking to implement a feature where when one user initiates a conversation in the chat, an email is automatically sent out to notify other chat users. The recipient can then reply directly from their email and have it added to the chat conversation. I am working with angular meteor for this project. What would be the best approach to achieve this? Are there APIs available in sendgrid or mailgun that can assist in handling incoming emails and adding them to the chat? Or should I consider creating POST/GET methods to accept incoming emails and save replied text upon button click?

Answer №1

If you want sendgrid to make a REST api call to your server upon receiving an incoming email, follow these steps:

When sending an email, designate the reply-to email as something like @chat-reply.myserver.com

Then, create an endpoint in your server code to handle these requests. Your code will have to search for the conversation based on the incoming address and then save a record in the chat.

Below is some sample code:

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

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

// This needs to run on both the server and client, hence not in the routing.js file
//   which only runs on the client.
//   Handles inbound emails for loop replies
//
// This is a RESTAPI end point called by sendgrid,
//   any email to <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbb3b3b3b38ba8a3aabfe6b9aebba7b2e5a6b2b8aeb9bdaeb9e5a8a4a6e5aabe">[email protected]</a> will land here. Our task
//   is to parse it, determine the related loop, and store it as a message in the database
//
Router.route('/inbound', function () {

    // Use formidable because SendGrid inbound data is encoded as multipart/form-data
    const form = new formidable.IncomingForm();
    // Meteor bind environment 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 = []

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

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

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

      let cleanContent;
      if (content){
        // Logger.trace({content: content});
        // Regex removes html tags
        // 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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a CORS header issue while working with the Authorization header

Here is the code snippet I am currently working with: https://i.stack.imgur.com/DYnny.png Removing the Authorization header from the headers results in a successful request and response. However, including the Authorization header leads to an error. http ...

Issue encountered while sending HTML email using Mailgun Python API

My current setup allows me to send text emails with the Mailgun Python API without any issues: def send_simple_message(email_text, attachment_filename=""): requests.post("https://api.mailgun.net/v3/mydomain.in/messages", auth=("api", "key-1234"), fi ...

How to interact with AngularJS drop-down menus using Selenium in Python?

I have been working on scraping a website to create an account. Here is the specific URL: Upon visiting the site, you need to click on "Dont have an account yet?" and then click "Agree" on the following page. Subsequently, there are security questions th ...