55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { onRequest } from "firebase-functions/v2/https";
|
|
import * as logger from "firebase-functions/logger";
|
|
const formData = require('form-data');
|
|
const Mailgun = require('mailgun.js');
|
|
const { convert } = require('html-to-text');
|
|
const twilio = require('twilio')
|
|
|
|
export const sendEmail = onRequest((request, response) => {
|
|
const mailgun = new Mailgun(formData);
|
|
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
|
|
|
const toAddress = request.body.toAddress;
|
|
const subject = request.body.subject;
|
|
const message = request.body.message;
|
|
const options = {
|
|
wordwrap: 130,
|
|
};
|
|
|
|
const textMessage = convert(message, options);
|
|
mailGunClient.messages.create(process.env.MAILGUN_SERVER, {
|
|
from: process.env.MAILGUN_FROM_ADDRESS,
|
|
to: toAddress,
|
|
subject: subject,
|
|
text: textMessage,
|
|
html: message
|
|
}).then((res: any) => {
|
|
logger.info(res);
|
|
response.send(res);
|
|
}).catch((err: any) => {
|
|
logger.error(err);
|
|
response.send(err);
|
|
});
|
|
});
|
|
|
|
export const sendSMS = onRequest((request, response) => {
|
|
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
|
|
|
const { to, body } = request.body;
|
|
|
|
client.messages
|
|
.create({
|
|
body: body,
|
|
from: process.env.TWILIO_PHONE_NUMBER,
|
|
to: to
|
|
})
|
|
.then((message: any) => {
|
|
logger.info('SMS sent successfully:', message.sid);
|
|
response.json({ success: true, messageId: message.sid });
|
|
})
|
|
.catch((error: any) => {
|
|
logger.error('Error sending SMS:', error);
|
|
response.status(500).json({ success: false, error: error.message });
|
|
});
|
|
});
|