DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Keep Your Application Secrets Secret
  • Generate Google Cloud API Credentials [Video]
  • Overcoming Some Pitfalls of the Google Maps API
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines

Trending

  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  1. DZone
  2. Data Engineering
  3. Databases
  4. Ping Alert Bot Using Mule 4 and Google Chat API

Ping Alert Bot Using Mule 4 and Google Chat API

These 8 steps will help you set up an automated alert bot with Mule 4 to ping a channel in the Google Chat app, letting you keep support personnel on notice.

By 
Vigneshwaran Kumaravel user avatar
Vigneshwaran Kumaravel
·
Dec. 10, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

Mulesoft to Google Chat

In this article, we will explore how we can send asynchronous messages in Google chat rooms with the help of Mule 4 and Google Chat API. The Anypoint Studio version used will be version 7.6, and the Mule runtime version will be 4.3 for this demonstration. It will be better to use the same version of Anypoint Studio and Mule runtime if you are planning to try to build the project. 

Prerequisites:

  1. Gmail Business Account
  2. Anypoint Studio 7.6
  3. ObjectStore Connector

Google Chat Application Google Chat

This comes with a Gmail business account, or you can get a trial on this feature for 14 days.

From direct messages to group conversations, Google Chat helps teams and businesses collaborate fluidly and efficiently from anywhere. Securely connect with anyone you work with, and take group work to the next level with shared chat, files and tasks.

Step 1: Create a Google chat room in the Google Chat app. Instructions from Google:

  1. Sign in to Chat or Gmail.
  2. Next to Rooms, click Create or Find a room > Create room.
  3. Enter a room name.
  4. (Optional) In the top left, click Choose an emoji . If you don't select an emoji, a default avatar is used.
  5. Enter names or email addresses of people and groups you want to add. You can also select from suggestions. Suggestions include everyone in your organization, even if they don't have Chat.

Step 2: Create a webhook and copy the incoming webhook URL to use in our flow to send messages asynchronously.

How to create and get incoming webhook URLs.

Webhook interface

Step 3: Add Object Store Connector in your studio from exchange if it's not already present.

Adding a dependency menu

Step 4: Add NetworkUtils.java class in the src/main/java folder of the project. The java file is provided by Mulesoft in net-tools-v2.2.0. You can download the java file from here [github-repository] and place it in your project location.

example folder explorer

Step 5: Call the java function inside your transform message component, which will ping the URL you want to access and give the ping output (if packet loss is there or not).

XML
 




x
20


 
1
<ee:transform doc:name="ping" doc:id="ede12d00-c3a8-4096-992c-715397fe1f4d">
2
            <ee:message>
3

          
4
            </ee:message>
5
            <ee:variables>
6
                <ee:set-variable variableName="host"><![CDATA[%dw 2.0
7
output application/java
8
---
9
payload]]></ee:set-variable>
10
                    <ee:set-variable variableName="ping-output" ><![CDATA[%dw 2.0
11
output application/java
12
import java!com::mulesoft::tool::network::NetworkUtils
13
---
14
//NetworkUtils::ping(attributes.queryParams.host)
15
NetworkUtils::ping(payload)
16
]]></ee:set-variable>
17
            </ee:variables>
18
        
19
</ee:transform>
20
        


Step 6: Extract the packet loss from the ping-output (text format) into a number.

XML
 




xxxxxxxxxx
1
13


 
1
<ee:transform doc:name="status check" doc:id="349ee048-0027-4f34-88f8-786388030aee">
2
            <ee:message>
3
                <ee:set-payload><![CDATA[%dw 2.0
4
import * from dw::core::Strings
5
var packetLoss=if(payload contains "packet loss")replace(trim(substringAfterLast(substringBefore(payload,"packet loss"),",")),"%") with "" else 0
6
output application/json
7
---
8
packetLoss as Number
9

          
10
]]></ee:set-payload>
11
            </ee:message>
12
        </ee:transform>
13
            


Step 7: Validate if the packet loss is greater than 50% with the help of the validation module and if yes, then trigger a request to our Google chat room with ping output using incoming webhook URL.

XML
 




xxxxxxxxxx
1
33


 
1
<try doc:name="Try" doc:id="6c6f8e9a-a147-4539-bcb1-3c2c91bc0927" >
2
            <ee:transform doc:name="payload &gt; 50" doc:id="3a9f533a-979b-4ebd-a74e-6a4b5f786e5f" >
3
                <ee:message >
4
                    <ee:set-payload ><![CDATA[%dw 2.0
5
output application/java
6
---
7
if (payload > 50) false else true ]]></ee:set-payload>
8
                </ee:message>
9
                <ee:variables >
10
                </ee:variables>
11
            </ee:transform>
12
            <logger level="INFO" doc:name="flag" doc:id="f5137316-2dd2-4a64-8733-909c3d2c8697" message="flag:#[payload]"/>
13
            <validation:is-true doc:name="Is true" doc:id="c1a131c4-b817-43ee-9674-3796c93085e8" expression="#[payload]" message="packet loss more than 50 %"/>
14
            <error-handler >
15
                <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="baad0777-48cc-40a4-800b-d5243b8882cf" type="ANY">
16
                    <logger level="INFO" doc:name="Error" doc:id="96937a4e-c9d5-4941-afa8-c1fe1fd30708" message="cached inside try scope:#[error]" />
17
                    <ee:transform doc:name="Error Payload" doc:id="d3097f03-b29d-410f-96d4-86aa0ce72e92" >
18
                        <ee:message >
19
                            <ee:set-payload ><![CDATA[%dw 2.0
20
output application/json
21
---
22
{
23
    text:vars."host" ++ "|" ++ vars."ping-output"
24
}
25
]]></ee:set-payload>
26
                        </ee:message>
27
                    </ee:transform>
28
                    <logger level="INFO" doc:name="input to chatbot" doc:id="b922d3dc-b11e-4c15-83f1-a3c7bf7a17d6" message="input to chatbot:#[payload]"/>
29
                    <flow-ref doc:name="google-chatbot-pocFlow" doc:id="b1336822-2fb1-4b8f-bc7b-f62bdea88173" name="google-chatbot-pocFlow"/>
30
                </on-error-continue>
31
            </error-handler>
32
        </try>
33
        


Step 8: Configure an Object Store to store the thread name to post the ping alerts in the same thread when sending the message and configure HTTP request connector to push messages with incoming webhook URL.

XML
 




xxxxxxxxxx
1
38


 
1
<flow name="google-chatbot-pocFlow" doc:id="16fbfc23-e766-4067-a66f-51f301195cd6" >
2
        <set-variable value="#[payload]" doc:name="input msg" doc:id="f9066a33-2d82-40ce-91a5-8f3ebf8192a2" variableName="inputmsg" mimeType="application/json"/>
3
        <logger level="INFO" doc:name="Logger" doc:id="704a19b9-7397-4397-9d19-20cb8b84df59" message='inputmsg:#[vars.inputmsg]'/>
4
        <try doc:name="Try" doc:id="ae3e8b0a-d79a-4f9b-a053-10b2fd7efb54" >
5
            <os:retrieve doc:name="Retrieve thread name" doc:id="0c353436-d5eb-4bad-9111-87e22eb9c285" key="thread_id" objectStore="Object_store" target="thread_name">
6
        </os:retrieve>
7
            <error-handler >
8
                <on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="ba1911d5-4c7c-42d0-833d-ee8fc96b435d" >
9
                    <logger level="INFO" doc:name="Logger" doc:id="01eb8ecf-12d1-4ab7-baf6-d337255a77d5" message="#[error.detailedDescription]"/>
10
                </on-error-continue>
11
            </error-handler>
12
        </try>
13
        <ee:transform doc:name="Transform Message" doc:id="0250f560-e8a9-49ad-ac7c-8b84e2e73462" >
14
            <ee:message >
15
                <ee:set-payload ><![CDATA[%dw 2.0
16
var thread=(thread:{
17
    name:vars.thread_name
18
}) if(vars.thread_name != "" and vars.thread_name != null)
19
output application/json
20
---
21
if(thread != null) (vars.inputmsg default "") ++ (thread default "") else vars.inputmsg default ""]]></ee:set-payload>
22
            </ee:message>
23
        </ee:transform>
24
        <logger level="INFO" doc:name="Logger" doc:id="f0459852-2dcc-41ed-bc72-4776407851ba" message="before chatbot:#[payload]"/>
25
        <http:request method="POST" doc:name="Send Bot Message" doc:id="98736f6a-ef5c-420a-8f3c-23d8ce924cee" url="${incoming.webhook.url}" doc:description="Using Webhhok URL we are sending messages from mule applications to google chat room for scenarios like server down which needs urgent action.">
26
            <http:headers ><![CDATA[#[output application/java
27
---
28
{
29
    "Content-Type" : "application/json",
30
    "charset" : "UTF-8"
31
}]]]></http:headers>
32
        </http:request>
33
        <logger level="INFO" doc:name="Logger" doc:id="bcc25906-55aa-450e-935f-eacb3ae62d00" message="REsponse:#[payload]"/>
34
        <os:store doc:name="Store thread name" doc:id="c7896666-ce67-4699-adc8-13b6d5be30ac" key="thread_id" objectStore="Object_store">
35
            <os:value ><![CDATA[#[payload.thread.name]]]></os:value>
36
        </os:store>
37
    </flow>
38

          


 

Use Case

  • You can use these types of bots to send alerts to on-call personnel or a support person to take proactive action.
  • This bot can also be used to send alerts into chats when certain business transactions complete.

To download the project, you can use this GitHub URL.

Let's give to the community.

API Google (verb)

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Generate Google Cloud API Credentials [Video]
  • Overcoming Some Pitfalls of the Google Maps API
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook