Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / serverless

Serverless Experiments with Google Cloud Platform: Part 3

5.00/5 (3 votes)
15 Jul 2017CPOL3 min read 11K  
Connecting Functions with other web services

7.1. Calling Function by Timer

Let's imagine that you want to send a weekly newsletter. Assuming that you know how to automate its contents (what about to put your recent liked reads there? https://www.codeproject.com/Articles/1159363/Export-Selected-Items-from-RSS-Reader-using-Micros), after the first two parts of this series, writing Function which sends email shouldn't be a big problem.

But how to arrange regular calls of your Function? Of course, you still don't want to pay for full-time server which will call you whenever you want.

One of the cheap possibilities is use of IFTTT.com or its alternatives.

IFTTT (IF This Than That) allows creating so called Applets by combining various Services.

Create free IFTTT account https://ifttt.com/join and being signed in go to your Applets

From there, create a New Applet.

Our Function will be triggered by Date & Time Service, which, except other possibilities (scroll down https://ifttt.com/date_and_time and click Date & Time triggers and actions), allows triggering (Trigger is This part of Applet) Every day of the week at the day and time you provide.

Click on This, find and choose Date & Time. Choosing Service first time usually needs to connect and setup it.

In the next step, Choose trigger Every day of the week at and fill its properties.

As that (Action part of Applet), use Webhooks' the only Action Make a web request.

As URL fill path to your Function (something like https://us-central1-ProjectName-123456.cloudfunctions.net/helloWorldFunction from the first lesson), Method POST and Content Type application/json (if your Function likes it). Than your Applet action's Body should look like this:

JavaScript
{"message": "Hello!"}

Because current trigger is Date & Time, URL or Body should contain also any of its properties (Add ingredient), for example:

JavaScript
{"message": "Hello, it's {{CheckTime}}!"} 

Every Applet has own log. Choose one from My Applets, go to its Configure (right-top gear icon) and click View activity log.

With correctly filled URL, after Applet run new entry will appear also in GCP Function's log.

Remember that example helloWorldFunction expects mandatory body.message:

JavaScript
if (req.body.message === undefined) {

  // This is an error case, as "message" is required.

  res.status(400).send('No message defined!');

Without posted message Applet should get into error state, sometimes already during its creation. In Applet's log instead of Applet ran appears Applet failed and after few minutes will IFTTT make one more attempt, also visible in both (IFTTT and GCP) logs.

7.1. Other Triggers

As could be seen during New Applet creation, triggering of Webhooks is not limited to Date & Time.

Trigger could be also mail (Email, Gmail, Office 365 Mail), so maybe you can use IFTTT instead of Mailgun from previous chapter.

Actual list contains services like RSS, Facebook, Twitter, Pocket, Calendar, GitHub, Weather or even Dishwasher, and grows.

Webhooks could be also a Trigger. This way, you can easily connect your Function with f.e. Twitter without knowing details of Twitter API or installing specific package knows it http://www.amitdeshmukh.co/getting-started-google-cloud-functions/. (For some services could exist more alternative packages.) Of course, connecting more servers means more delays and more possible failures. Anyway, it's worth trying it.

Looking at your Webhooks Settings and you will see a unique URL associated with it. It should look like https://maker.ifttt.com/use/abcdefghijklmnopqrstuvwxyz. Pushing Edit connection button will URL change.

Open your https://maker.ifttt.com/use/abcdefghijklmnopqrstuvwxyz in web browser and you will see page with

JavaScript
Your key is: abcdefghijklmnopqrstuvwxyz

information and form for testing three available fixedly named optional parameters:

JavaScript
With an optional JSON body of:

{ "value1" : "", "value2" : "", "value3" : "" }

Knowing your key, open https://maker.ifttt.com/trigger/makertest/with/key/abcdefghijklmnopqrstuvwxyz and you will see:

JavaScript
Congratulations! You've fired the makertest event

In log https://ifttt.com/activity/service/maker_webhooks, it is visible this way:

JavaScript
EventName

makertest

OccurredAt

June 41, 2017 at 27:98PM

Value1

(none)

Value2

(none)

Value3

(none)

Don't be fooled by log. Variable names start with small letters. Setting them in URL should be this way:

not:

7.2. Alternative

Instead of IFTTT, you can try Zapier.

Applets are here named Zaps, timer is Schedule, Webhooks also Webhooks.

Connections to third party services work similarly to IFTTT's, but lists of them are not equal. Some features are in Free Plan limited.

What is unique is their email Parser. Emails send (forwarded) to parser-related email address (something like abcdef12@robot.zapier.com) are compared with predefined pattern and selected parts could become text variables used into Zaps. Sometimes, it could completely eliminate the need of writing your own email parser as GCP Function.


End of Part Three.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)