Update index.ts
This commit is contained in:
parent
a6a91826d5
commit
c8a52d2b1c
@ -1,59 +1,59 @@
|
|||||||
import { onRequest } from "firebase-functions/v2/https";
|
import { onRequest } from "firebase-functions/v2/https";
|
||||||
import { Request } from "firebase-functions/v2/https";
|
import { Request } from "firebase-functions/v2/https";
|
||||||
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
|
|
||||||
import * as admin from 'firebase-admin';
|
import * as admin from 'firebase-admin';
|
||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import * as logger from "firebase-functions/logger";
|
import * as logger from "firebase-functions/logger";
|
||||||
|
|
||||||
|
const functions = require('firebase-functions');
|
||||||
const formData = require('form-data');
|
const formData = require('form-data');
|
||||||
const Mailgun = require('mailgun.js');
|
const Mailgun = require('mailgun.js');
|
||||||
const { convert } = require('html-to-text');
|
const { convert } = require('html-to-text');
|
||||||
const twilio = require('twilio')
|
const twilio = require('twilio')
|
||||||
|
|
||||||
export const sendEmail = onRequest((request: Request, response: express.Response) => {
|
export const sendEmail = onRequest((request: Request, response: express.Response) => {
|
||||||
const mailgun = new Mailgun(formData);
|
const mailgun = new Mailgun(formData);
|
||||||
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
const mailGunClient = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY });
|
||||||
|
|
||||||
const toAddress = request.body.toAddress;
|
const toAddress = request.body.toAddress;
|
||||||
const subject = request.body.subject;
|
const subject = request.body.subject;
|
||||||
const message = request.body.message;
|
const message = request.body.message;
|
||||||
const options = {
|
const options = {
|
||||||
wordwrap: 130,
|
wordwrap: 130,
|
||||||
};
|
};
|
||||||
|
|
||||||
const textMessage = convert(message, options);
|
const textMessage = convert(message, options);
|
||||||
mailGunClient.messages.create(process.env.MAILGUN_SERVER, {
|
mailGunClient.messages.create(process.env.MAILGUN_SERVER, {
|
||||||
from: process.env.MAILGUN_FROM_ADDRESS,
|
from: process.env.MAILGUN_FROM_ADDRESS,
|
||||||
to: toAddress,
|
to: toAddress,
|
||||||
subject: subject,
|
subject: subject,
|
||||||
text: textMessage,
|
text: textMessage,
|
||||||
html: message
|
html: message
|
||||||
}).then((res: any) => {
|
}).then((res: any) => {
|
||||||
logger.info(res);
|
logger.info(res);
|
||||||
response.send(res);
|
response.send(res);
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
response.send(err);
|
response.send(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
export const sendSMS = onRequest((request: Request, response: express.Response) => {
|
export const sendSMS = onRequest((request: Request, response: express.Response) => {
|
||||||
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);
|
||||||
const { to, body } = request.body;
|
const { to, body } = request.body;
|
||||||
client.messages
|
client.messages
|
||||||
.create({
|
.create({
|
||||||
body: body,
|
body: body,
|
||||||
from: process.env.TWILIO_PHONE_NUMBER,
|
from: process.env.TWILIO_PHONE_NUMBER,
|
||||||
to: to
|
to: to
|
||||||
})
|
})
|
||||||
.then((message: any) => {
|
.then((message: any) => {
|
||||||
logger.info('SMS sent successfully:', message.sid);
|
logger.info('SMS sent successfully:', message.sid);
|
||||||
response.json({ success: true, messageId: message.sid });
|
response.json({ success: true, messageId: message.sid });
|
||||||
})
|
})
|
||||||
.catch((error: any) => {
|
.catch((error: any) => {
|
||||||
logger.error('Error sending SMS:', error);
|
logger.error('Error sending SMS:', error);
|
||||||
response.status(500).json({ success: false, error: error.message });
|
response.status(500).json({ success: false, error: error.message });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -64,9 +64,8 @@ interface Invitation {
|
|||||||
invitedByName: string;
|
invitedByName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const sendInvitationNotification = onDocumentCreated(
|
export const sendInvitationNotification = functions.region('asia-south1').firestore.document('notifications/{notificationId}')
|
||||||
'invitations/{invitationId}',
|
.onCreate(async (event: { data: { data: () => Invitation; }; params: { invitationId: any; }; }) => {
|
||||||
async (event) => {
|
|
||||||
const invitation = event.data?.data() as Invitation;
|
const invitation = event.data?.data() as Invitation;
|
||||||
const invitationId = event.params.invitationId;
|
const invitationId = event.params.invitationId;
|
||||||
|
|
||||||
@ -132,4 +131,4 @@ export const sendInvitationNotification = onDocumentCreated(
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
Loading…
Reference in New Issue
Block a user