0

Trying to send off a webhook to Slack whenever onWrite() is triggered directed toward my Firebase DB. Going off a few other posts/guides I was able to deploy the below code, but get the ReferenceError: Request is not defined error on execution. I can't figure out how to fix the Request is not defined.

const functions = require('firebase-functions');
const webhookURL = "https://hooks.slack.com/services/string/string";

exports.firstTest = functions.database.ref('first').onWrite( event => {
  return request.post(
    webhookURL,
    {json: {text: "Hello"}}
  );
});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Ranson Namba
  • 335
  • 1
  • 2
  • 15

1 Answers1

1

Calling your Cloud Function via an URL and sending back a response

By doing exports.firstTest = functions.database.ref('first').onWrite() you trigger your firstTest Cloud Function when data is created, updated, or deleted in the Realtime Database. It is called a background trigger, see https://firebase.google.com/docs/functions/database-events?authuser=0

With this trigger, everything happens in the back-end and you do not have access to a Request (or a Response) object. The Cloud Function doesn't have any notion of a front-end: for example it can be triggered by another back-end process that writes to the database. If you want to detect, in your front-end, the result of the Cloud Function (for example the creation of a new node) you would have to set a listener to listen to this new node location.


If you want to call your function through an HTTP request (possibly from your front-end, or from another "API consumer") and receive a response to the HTTP Request, you need to use another type of Cloud Function, the HTTP Cloud Function, see https://firebase.google.com/docs/functions/http-events. See also the other type of Cloud Function that you can call directly: the Callable Cloud Functions.


Finally, note that:



Calling, from your Cloud Function, an external URL

If you want, from a Cloud Function, to call an external URL (the Slack webhook mentioned in your question, for example) you need to use a library like request-promise (https://github.com/request/request-promise).

See How to fetch a URL with Google Cloud functions? request? or Google Cloud functions call URL hosted on Google App Engine for some examples

Important: Note that you need to be on the "Flame" or "Blaze" pricing plan.

As a matter of fact, the free "Spark" plan "allows outbound network requests only to Google-owned services". See https://firebase.google.com/pricing/ (hover your mouse on the question mark situated after the "Cloud Functions" title)

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Great answer, thank you so much this helped and I was able to successfully get a "hello world" out to my slack channel. I'm trying to get a payload out from the snapshot, but whenever I try to store the snapshot.val(), my function errors on execution w/ val is not a function almost identical to the bottom code blocks at https://stackoverflow.com/a/52526632/4013537 – Ranson Namba May 11 '19 at 11:30
  • Pretty sure I found where I went wrong according to this thread https://stackoverflow.com/a/50111454/4013537 – Ranson Namba May 11 '19 at 11:41
  • Yes, as said in my answer «With .onWrite( event => {}), you are using the old syntax, see https://firebase.google.com/docs/functions/beta-v1-diff?authuser=0». You need to (possibly) update your libraries and adapt your code, as explained in this doc. – Renaud Tarnec May 11 '19 at 11:45