云计算
现在阅读
Serverless experiments with Google Cloud Platform, Part 3
0

Serverless experiments with Google Cloud Platform, Part 3

由 ultracpy2018年1月20日

7.1. Calling Function by timer

Let’s imagine that you want to send 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 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 https://ifttt.com/search/services.

Create free IFTTT account https://ifttt.com/join and being signed in go to your Applets https://ifttt.com/my_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 at 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‘  https://ifttt.com/maker_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

{"message": "Hello!"}

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

{"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

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 to try it.

Looking at your Webhooks Settings https://ifttt.com/services/maker_webhooks/settings you will see 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

Your key is: abcdefghijklmnopqrstuvwxyz

information and form for testing three available fixedly named optional parameters

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

Congratulations! You've fired the makertest event

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

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

https://maker.ifttt.com/trigger/makertest/with/keyabcdefghijklmnopqrstuvwxyz?value1=hallo

not

https://maker.ifttt.com/trigger/makertest/with/keyabcdefghijklmnopqrstuvwxyz?Value1=hallo

7.2. Alternative

Instead of IFTTT you can try Zapier https://zapier.com.

Applets are here named Zaps, timer is Shedule https://zapier.com/zapbook/schedule/, Webhooks also Webhooks https://zapier.com/zapbook/webhook/.

Connections to third party services work similarly to IFTTT‘s, but lists of them are not equal. Some features are in Free Plan https://zapier.com/app/billing/plans limited.

What is unique is their email Parser https://parser.zapier.com/. 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 need of writing own email parser as GCP Function.


End of Part Three.

出处:https://www.codeproject.com/Articles/1196669/Serverless-experiments-with-Google-Cloud-Platfor

关于作者
ultracpy
评论

    你必须 登录 提交评论