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.
Join the DZone community and get the full member experience.
Join For FreeIn 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:
- Gmail Business Account
- Anypoint Studio 7.6
- ObjectStore Connector
Google Chat Application
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:
- Sign in to Chat or Gmail.
- Next to Rooms, click Create or Find a room > Create room.
- Enter a room name.
- (Optional) In the top left, click Choose an emoji . If you don't select an emoji, a default avatar is used.
- 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.
Step 3: Add Object Store Connector in your studio from exchange if it's not already present.
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.
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).
<ee:transform doc:name="ping" doc:id="ede12d00-c3a8-4096-992c-715397fe1f4d">
<ee:message>
</ee:message>
<ee:variables>
<ee:set-variable variableName="host"><![CDATA[%dw 2.0
output application/java
---
payload]]></ee:set-variable>
<ee:set-variable variableName="ping-output" ><![CDATA[%dw 2.0
output application/java
import java!com::mulesoft::tool::network::NetworkUtils
---
//NetworkUtils::ping(attributes.queryParams.host)
NetworkUtils::ping(payload)
]]></ee:set-variable>
</ee:variables>
</ee:transform>
Step 6: Extract the packet loss from the ping-output (text format) into a number.
xxxxxxxxxx
<ee:transform doc:name="status check" doc:id="349ee048-0027-4f34-88f8-786388030aee">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
import * from dw::core::Strings
var packetLoss=if(payload contains "packet loss")replace(trim(substringAfterLast(substringBefore(payload,"packet loss"),",")),"%") with "" else 0
output application/json
---
packetLoss as Number
]]></ee:set-payload>
</ee:message>
</ee:transform>
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.
xxxxxxxxxx
<try doc:name="Try" doc:id="6c6f8e9a-a147-4539-bcb1-3c2c91bc0927" >
<ee:transform doc:name="payload > 50" doc:id="3a9f533a-979b-4ebd-a74e-6a4b5f786e5f" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
if (payload > 50) false else true ]]></ee:set-payload>
</ee:message>
<ee:variables >
</ee:variables>
</ee:transform>
<logger level="INFO" doc:name="flag" doc:id="f5137316-2dd2-4a64-8733-909c3d2c8697" message="flag:#[payload]"/>
<validation:is-true doc:name="Is true" doc:id="c1a131c4-b817-43ee-9674-3796c93085e8" expression="#[payload]" message="packet loss more than 50 %"/>
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="baad0777-48cc-40a4-800b-d5243b8882cf" type="ANY">
<logger level="INFO" doc:name="Error" doc:id="96937a4e-c9d5-4941-afa8-c1fe1fd30708" message="cached inside try scope:#[error]" />
<ee:transform doc:name="Error Payload" doc:id="d3097f03-b29d-410f-96d4-86aa0ce72e92" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
text:vars."host" ++ "|" ++ vars."ping-output"
}
]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="input to chatbot" doc:id="b922d3dc-b11e-4c15-83f1-a3c7bf7a17d6" message="input to chatbot:#[payload]"/>
<flow-ref doc:name="google-chatbot-pocFlow" doc:id="b1336822-2fb1-4b8f-bc7b-f62bdea88173" name="google-chatbot-pocFlow"/>
</on-error-continue>
</error-handler>
</try>
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.
xxxxxxxxxx
<flow name="google-chatbot-pocFlow" doc:id="16fbfc23-e766-4067-a66f-51f301195cd6" >
<set-variable value="#[payload]" doc:name="input msg" doc:id="f9066a33-2d82-40ce-91a5-8f3ebf8192a2" variableName="inputmsg" mimeType="application/json"/>
<logger level="INFO" doc:name="Logger" doc:id="704a19b9-7397-4397-9d19-20cb8b84df59" message='inputmsg:#[vars.inputmsg]'/>
<try doc:name="Try" doc:id="ae3e8b0a-d79a-4f9b-a053-10b2fd7efb54" >
<os:retrieve doc:name="Retrieve thread name" doc:id="0c353436-d5eb-4bad-9111-87e22eb9c285" key="thread_id" objectStore="Object_store" target="thread_name">
</os:retrieve>
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="ba1911d5-4c7c-42d0-833d-ee8fc96b435d" >
<logger level="INFO" doc:name="Logger" doc:id="01eb8ecf-12d1-4ab7-baf6-d337255a77d5" message="#[error.detailedDescription]"/>
</on-error-continue>
</error-handler>
</try>
<ee:transform doc:name="Transform Message" doc:id="0250f560-e8a9-49ad-ac7c-8b84e2e73462" >
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
var thread=(thread:{
name:vars.thread_name
}) if(vars.thread_name != "" and vars.thread_name != null)
output application/json
---
if(thread != null) (vars.inputmsg default "") ++ (thread default "") else vars.inputmsg default ""]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Logger" doc:id="f0459852-2dcc-41ed-bc72-4776407851ba" message="before chatbot:#[payload]"/>
<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.">
<http:headers ><![CDATA[#[output application/java
---
{
"Content-Type" : "application/json",
"charset" : "UTF-8"
}]]]></http:headers>
</http:request>
<logger level="INFO" doc:name="Logger" doc:id="bcc25906-55aa-450e-935f-eacb3ae62d00" message="REsponse:#[payload]"/>
<os:store doc:name="Store thread name" doc:id="c7896666-ce67-4699-adc8-13b6d5be30ac" key="thread_id" objectStore="Object_store">
<os:value ><![CDATA[#[payload.thread.name]]]></os:value>
</os:store>
</flow>
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.
Opinions expressed by DZone contributors are their own.
Comments