This article will show you how to trigger a new email when a message is sent in your App. The logic would be the same for SMS or Push notifications if you are unable to use our Push service or need alternative business logic. To accomplish this, we will connect the Stream Webhook to a Server-Side Endpoint, add logic for consuming Webhook, and use an email package called nodemailer. If you are interested in integrating SMS/E-mail with Stream Chat, then this is a great place to start.
Note: with Stream, SMS/E-mail services require the use of a third party package or service. As mentioned above, we will use a free package called nodemailer in this example.
Step 1: Add your Server-Side URL to the Dashboard
Place the url to your server-side endpoint, which will consume the Webhook events, in your Dashboard. You can navigate here via the Dashboard > App > Chat (drop down) > Overview > Chat Events. In this example, we will only be using the standard Webhook (first Webhook listed).
Step 2: Create the desired logic for handling Webhook events on your Server
Write a script on your server to consume and organize the incoming payload and perform any desired actions. The below example is in node.js and uses a local express server. If any new events coming in from the Webhook are the result of a new message, then this script will send an email to the desired address. The email body will contain this new message text. Make sure to replace the email service if you wish to and add your own email credentials.
const cors = require("cors");
const nodemailer = require('nodemailer');
const port = process.env.PORT || 600;
const express = require("express");
const app = express();
// Middleware
app.use(cors());
// Webhook handler
app.post("/", (req, res) => {
let body="";
req.on("data", (chunk) => {
body+=chunk;
});
// Payload from Stream
req.on("end", async () => {
let parsedBody = JSON.parse(body);
// Verifying there is a message in the payload
if(parsedBody.message==undefined){
return
}
// Here you can send an email, integrate an SMS service,
// or perform any other desired actions based on your Webhook events data
try {
let emailMessage = parsedBody.message.text
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'exampleEmailAddress@getstream.io',
pass: 'example password'
}
});
let mailOptions= {
from: 'sender@getstream.io',
to: 'recipient@getstream.io',
subject: 'subject line',
text: "Hi!"+"\n\n"+"You missed this message."+"\n\n"+JSON.stringify(emailMessage)
}
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: '+info.response);
}
});
} catch (error) {
console.log(error);
}
res.status(200).send("OK");
});
});
app.listen(port, () => {
console.log(`server running on port ${port}`);
});
Step 3: Tweak and Add Your Required Functionality
Add in logic and parameters that work best for your use case. If you have any exciting examples of integrating with E-mail/SMS or any other questions, please reach us at https://getstream.io/contact/support/!
Comments
0 comments
Please sign in to leave a comment.