firebase function triggered on email received - firebase

Is there a way to trigger a firebase function or create an item in Firestore when an email is received on Gmail or Outlook.
I'm trying to recreate on of ServiceNow's features where in if the helpdesk email address has received an email, it will automatically create a ticket (to firestore) out of it.
Thank you!

Here is one possible solution for Gmail (untested):
According to the Gmail API documentation:
The Gmail API provides server push notifications that let you watch
for changes to Gmail mailboxes... Whenever a mailbox changes, the Gmail
API notifies your backend server application.
...
The Gmail API uses the Cloud Pub/Sub API to deliver push notifications.
Since you can trigger a Cloud Function whenever a new Pub/Sub message is sent to a specific topic you can do as follows:
Set up a Cloud Pub/Sub client.
Using your Cloud Pub/Sub client, create the topic that the Gmail API should send notifications to.
Configure the Gmail account(s) to send notifications for mailbox updates
Write a Cloud Function that is triggered whenever a new Pub/Sub message is sent to this topic and execute the desired Business Logic (e.g. write to Firestore)
For Outlook, this SO answer indicates that it should be possible to call an API when a new mail is received. You could either call an HTTPS Cloud Function or directly the Firestore REST API.

Related

Get messaging tokens from Go admin backend when adding users to Firebase, over upon registration in Javascript

I haven't seen any examples of this, but I want to purely interact with Firebase through the backend, and not the frontend with Javascript.
I have auth tokens being minted on my Go backend when a new user is added and then these users are written into a mongo database.
What I want is to be able to get a messaging token for my users, and then add it to their user document in mongo, that'll be used to send messages through the backend.
The reasoning is that we don't want to have to communicate with Firebase on our frontend.
Is this even possible?
If you want to send a message directly to a device with Firebase Cloud Messaging, you will definitely need some information from the client. There is no avoiding following the setup instructions on the client. In particular, you will have to handle the registration token on the client and send it to your backend so it can send the messages.
The Firebase Authentication token will not be useful to you at all for sending messages. FCM doesn't send messages to users - it sends messages to devices (or topics). You will have to figure out for yourself which devices belong to which users.

Response of cloud function

I'm wondering if it is possible to get a response message when Firebase Cloud Function is executed succesfully.
The case is I'm trying to send an email, but I would like to make sure it is sent and received by the receiver.
If you use Sendgrid to send emails, as recommended by Firebase (see https://github.com/firebase/functions-samples/tree/master/quickstarts/email-users), you can use Sendgrid's Events Webhooks, see https://sendgrid.com/docs/for-developers/tracking-events/event/#engagement-events and https://sendgrid.com/docs/for-developers/tracking-events/getting-started-event-webhook/.
As explained in the last link above, "SendGrid's Event Webhook will notify a URL of your choice via HTTP POST with information about events that occur as SendGrid processes your email. "
You just have to setup an HTTP Cloud Function that will be called by Sendgrid for the events of your choice.

Enabling the notification feature for my firebase app

I have made an app with the use of firebase. It is basically a chat application in which you van send and receive the text and images. I want to add a functionality in it that whenever a user sends a msg, then another user should get a notification . When I try to send a notification through the firebase console, then it is working, but when a user messages through the app, then it is not showing any notification to another user. So, can anyone tell me that how can this functionality be achieved ?Also, provide some sample code to see how things are working
You need to use cloud functions for that:
https://firebase.google.com/docs/functions
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests
First, you can register the user to a topic:
FirebaseMessaging.getInstance().subscribeToTopic("news");
https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Then when the message is sent to the database, you can use onWrite() in cloud functions which is a database trigger to be able to send the notification.
https://firebase.google.com/docs/functions/database-events

Receiving Emails with Google Cloud Functions in Firebase

I'd like to build an email drop service. Is it anyhow possible to receive incoming emails with Google cloud functions, process them and store them in firebase db?
I'm thinking of something similar to Amazon simple email service SES in combination with lambda functions. Does any of the available email services like sendgrid, mailgun, postmark or any other has api's to trigger Google Cloud Functions on incoming emails?
Yes, services like Sendgrid and Mailgun allow you to set up webhooks for incoming mail. You can configure these webhooks to point to an HTTPS Cloud Function, at which point it will be invoked each time you get a new incoming email.

Push Notifications through Firebase

Yesterday Google has announced a new set of tools for Firebase, one of them was Notifications the ability to send notifications from server to devices which are using my app.
But can we now notify users when they receive a new message?
And if not, is there a way around to achieve this?
Looking at the documentation, this doesn't seem currently possible automatically. Here is a possible way to accomplish it "manually" with another server:
Subscribe a user to it's own user ID
In android
FirebaseMessaging.getInstance().subscribeToTopic("InsertUserIDHere");
In IOS
[[FIRMessaging messaging] subscribeToTopic:#"/topics/InsertUserIDHere"];
Setup an outside server that checks every sent messages. When a message is sent, the server should create a notification that includes the recipient's user ID as the topic.
Look here for more info.
You can send messages to group of users (targeting a specific "topic") or to a single device (targeting a Firebase Cloud Messaging token).
To subscribe a device to a topic use:
FirebaseMessaging.getInstance().subscribeToTopic("topicName");
To obtain the device token use (*1) :
FirebaseInstanceId.getInstance().getToken();
Then you can use the Firebase Notificaitons web console, or the FCM server API if you want to send messages from your server.
See: https://firebase.google.com/docs/cloud-messaging/downstream#sending_topic_messages_from_the_server
Notes:
[1] getToken() can return null if the token is not yet available.
You can use the callback onTokenRefresh() to be notified when the token is available and when the token is rotated.
See: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

Resources