In this blog I will show how to use the event routing feature of LaMa Cloud and use this to post the events to a Teams channel. This is an example of an event-driven automation where we are taking an action as the event occurs. In this case the action is just to post a message to Teams but the action can also be something else. In a future blog I plan to show a different action or a series of actions.
Sample is available at GitHub
Let us start with the basics to ensure we can easily post to a Teams channel. We do this by creating an incoming webhook for your channel. Refer to this Microsoft article.
Test the webhook in Postman.
The message is posted.
Integrating LaMa Cloud with SAP Event Mesh is described in the guide in this section. The key steps are summarized below.
{ "emname": "<your instance name>", "namespace": "default/sap.lmc/<SAP Landscape Management Cloud tenant ID>", "options": { "management": true, "messaging": true, "messagingrest": true }, "resources": { "units": "10" }, "rules": { "queueRules": { "publishFilter": [ "${namespace}/*" ], "subscribeFilter": [ "${namespace}/*" ] }, "topicRules": { "publishFilter": [ "${namespace}/*" ], "subscribeFilter": [ "${namespace}/*" ] } }, "version": "1.1.0", "xs-security": { "oauth2-configuration": { "credential-types": [ "binding-secret" ] } } }
Following topics were added for this example:
We will use Client Credentials in this example for configuring the Event Routing in LaMa Cloud.
If you forgot to activate the broker during creation (as I did), you can activate it manually afterwards.
Now if there is an event related to STARTSERVICE or STOPSERVICE in LaMa Cloud the event will be sent to the SAP Event Mesh queue we created.
This section covered how the events get sent to a queue.
Now that we know our queue is receiving events from LaMa Cloud, we can set up a receiver. We have two options for this:
We will cover the second option here as we want immediate forwarding of the event to our receiver so that we can take an action. As mentioned earlier the action we will take in this example is to post a message to MS Teams.
A very small and basic Python web app is going to act as our receiver and the sample code is below and also on GitHub. Note that this is for demo purposes only and we have not added any authentication for it.
import os import json import requests from flask import Flask, request app = Flask(__name__) port = int(os.environ.get('PORT', 3000)) stored_payload = None teams_webhook_url = '<URL for MS Teams Webhook>' @app.route('/', methods=['GET', 'POST']) def receive_post(): global stored_payload if request.method == 'POST': try: stored_payload = request.get_json() # Store the JSON payload from LaMa Cloud send_to_teams(stored_payload) # Send the payload to Microsoft Teams return 'Success' # Send a response indicating successful processing except Exception as e: return 'Error: {}'.format(str(e)), 400 # Send an error response if there was an issue else: if stored_payload: response = json.dumps(stored_payload, indent=2, separators=(',', ': ')) return response.replace('\n', '<br>') # Replace newlines with HTML line breaks for browser display else: return 'No JSON payload to show', 404 # Send a not found response if no payload is available def send_to_teams(payload): headers = {'Content-Type': 'application/json'} data = { 'text': 'LaMa Cloud JSON Payload:\n\n' + json.dumps(payload, indent=2) } response = requests.post(teams_webhook_url, headers=headers, json=data, verify=False) if response.status_code != 200: raise Exception('Failed to send payload to Teams. Status Code: {}'.format(response.status_code)) if __name__ == '__main__': app.run(host='0.0.0.0', port=port)
To deploy the above to a cloud platform such as SAP BTP Cloud Foundry you will also need the following files. Note that application name should not have "_" in the name.
manifest.yml:
--- applications: - name: my-receiver random-route: true path: ./ memory: 128M buildpack: python_buildpack command: python server.py
requirements.txt:
Flask==2.2.5 flask requests
runtime.txt (change the version to match what you have):
python-3.10.11
For deployment to SAP BTP Cloud Foundry refer to this blog. Note down the URL generated.
Go to a browser to make sure the app is responding. Since no event has been sent yet, it will say that no JSON payload is available.
Now we will configure a webhook for the Event Mesh queue so that any event coming in is immediately sent to our receiver app.
For more information about webhook management refer to the help document
Also the browser should show one of the payloads.
Now that the webhook is setup, any new events coming to the queue will route right away to the app and posted to Teams.
You might wonder why didn't we just use the Teams URL when creating the webhook in SAP Event Mesh and bypass the receiver app altogether. There are a couple of reasons for this. Firstly Teams is picky about the format being sent and the events generated by LaMa Cloud would not work so we have to use a middleman to send it correctly. Secondly we might want to add logic in our app to take other actions based on the event received.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
24 | |
9 | |
8 | |
7 | |
7 | |
6 | |
6 | |
6 | |
6 | |
6 |