如何将Flows 与 Google Vision API 和 Google Cloud Functions 结合使用

如何将Flows 与 Google Vision API 和 Google Cloud Functions 结合使用

如何将Flows 与 Google Vision API 和 Google Cloud Functions 结合使用

Oct 6, 2020

出版商

出版商

Bird

Bird

-

类别

类别

流程建设者

流程建设者

Ready to see Bird
in action?

Ready to see Bird
in action?

How to use Flows with Google Vision API and Google Cloud Functions

This is a simple demonstration using 谷歌云功能 and Flows to do image recognition on an image sent on Telegram.


Flows 及其他

作为Flows 的开发人员,我经常思考我们的用户是谁,他们为什么使用Flows ,他们需要什么来实现他们的目标;然后,我们需要实现哪些功能来为这些用户提供最佳服务。


Flows is a powerful drag-and-drop automation engine for creating communication flows. We initially conceived it as a no-code solution, but we found many users could get really powerful behavior writing some code for specific use-cases.   These bits of code can be inside Flows, or they can be 3rd party cloud functions like AWS Lambda functions or Google Cloud Functions


An interesting use case: Image Recognition 

在一个简短而有趣的示例中,我将向您展示如何实现一个可以识别热狗的应用程序。我们将在Flows 中建立一个流程,该流程将接收用户发送的图片,并判断用户是否发送了热狗。


For many of our customers, this type of image recognition can be very powerful. Imagine you run a delivery service and you wanted to verify successful deliveries automatically. Similar to what I’m going to show, you could use location data, photos of parcels, and even recipient signatures to create a verification flow in Flows. 


一个成功的计划

First, we will set up a cloud function, which receives a request with a URL to an image, then it uses an image recognition API to process the image,  and responds whether there is a hotdog in the image or not.


然后,我们将建立一个流程,通过消息通道(本例中为Telegram)接收用户的消息,执行上述云函数,并向用户回复他发送的图片中是否有热狗。


设置谷歌云功能

First,  we will need to set up a cloud function. To get started quickly, create a cloud function using this tutorial: https://cloud.google.com/functions/docs/quickstart-console. As a ‘Trigger’ choose HTTP trigger, execution environment: Node.js 10, and in the source code field insert 代码片断. It’s simple code, which checks whether the request contains JSON code and answers yes or no. 



Next, you’ll need to deploy this function. To test it inside Google Cloud Platform, follow steps from the tutorial. 

要从你的浏览器进行测试,请进入以下网址,插入你的功能的具体地址。


https://你的功能-地址.cloudfunctions.net/HotDogOrNot/?url=hello should return {“是热狗”: true} and the address https://你的功能-地址.cloudfunctions.net/HotDogOrNot should return {“isHotDog”: false}.


干得好!你建立了一个谷歌云功能。现在,我们需要使我们的云功能更加智能。


设置谷歌视觉API

To make it smarter let’s add image recognition. For this purpose we will use the Google Vision API. To get started, follow steps 1-4 of this tutorial: https://cloud.google.com/vision/docs/quickstart-client-libraries. In the tutorial you’ll activate the Vision API and create a service account to use it.


现在返回到你创建的云功能。切换 "环境变量、网络、超时和更多",在文件 "服务账户 "中选择你刚刚创建的VisionAPI服务账户。现在我们可以在我们的函数中访问Vision API了。



Now let’s change the code. On a “Package.json” tab, insert 此代码. It will add Google Vision API library as a dependency to your function, and on "Index.js" tab update existing code with 下面的代码片断.



What’s the difference compared 到 previous version? We added a request to VisionAPI, which returns the '标签'。 it found on the image.  Then we filter these labels by description: if it contains “hot dog” and if it has greater than 60% confidence in that label. If there is at least 1 label remaining after filtering, that means we found a hotdog on the image.


To understand how Google VisionAPI works and how the response looks like check their documentation, https://cloud.google.com/vision/docs


After that, deploy the new version of our function. To test it from your browser, find any image of a hotdog and save it's URL. Now go to URL of your function (inserting the correct address for your function) https://your-function-address.cloudfunctions.net/HotDogOrNot?url=url_to_image and replace the  “url_to_image” with a URL 到 found image. If there is a hotdog in the image, the page will return {“isHotDog”: true}.


现在让我们把这个函数连接到Flow Builder。


在Flows

Log in into the Bird Dashboard or 注册账户 if you don’t have one.


如果您是Flows 的新用户,并且没有设置任何channels ,则需要进入频道设置页面,选择设置 Telegram 频道。我之所以选择 Telegram 来做这个演示,是因为它的设置简单快捷。



现在我们有了一个可以在Flows 中使用的频道。访问Flows 页面,创建一个新的自定义流程,然后选择 "Telegram "频道触发器。



你会被转到一个流程页面,在那里你应该选择你的Telegram频道作为触发器,在我们的例子中是 "Hotdog"。请添加2个步骤。"获取变量 "和 "回复频道信息"。


Inside the “Fetch variables” step we will call our cloud function and retrieve response into variable “isHotDog” which will contain “true” or “false” as a response from the GoogleClound function. In URL field please insert URL to your function https://your-function-address.cloudfunctions.net/HotDogOrNot and fill all other fields as on the "Fetch variable step content" picture.


And inside the “Reply to channel message” step we will respond to the customer with a message containing the yes or no response. For that insert in "Reply with message" field the following text "Hotdog on the image? {{isHotDog}}".



If you have any trouble building the flow, you can export it from file: simple-hotdog-flow.json.



To test it, send an image to your Telegram bot.

So far, it looks cool! We created a small chat bot, which checks images customers sent. To make it prettier, let’s add some more steps as shown below: 



If you have any trouble building the flow, you can export it from file: advanced-hotdog-flow.json.

结果


While this is a fun example, we believe this type of functionality can be very useful for our users. 

If you want more features like this built-in in Flows, write to our support team to let us know. 

Your new standard in Marketing, Pay & Sales. It's Bird

ǞǞǞ right message -> to the right person ->right time.

By clicking "See Bird" you agree to Bird's 隐私声明.

Your new standard in Marketing, Pay & Sales. It's Bird

ǞǞǞ right message -> to the right person ->right time.

By clicking "See Bird" you agree to Bird's 隐私声明.