cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Unity Catalog Python UDF to Send Messages to MS Teams

SQLBob
New Contributor II

Good Morning All - 

This didn't seem like such a daunting task until I tried it. Of course, it's my very first function in Unity Catalog. 

Attached are images of both the UDF and example usage I created to send messages via the Python requests library within the UDF. I'll see if I can paste in the code below. 

The UDF is supplied with a URL and payload (JSON - Adaptative card for MS Teams). It should send the payload as a Teams message and works as intended when defined internally within a Python notebook. But when defined within UC and executed from a notebook, it sends no message and returns no errors.

I would appreciate it if one of you experts would eyeball this thing and pull me out of the weeds. I can't believe I'm the first one to try this although hours spent researching it has only resulted in references to defining the processing internally within the notebook.

====================  UDF ===========================

CREATE OR REPLACE FUNCTION mycatalog.myschema.udf_send_post_request(
url STRING,
payload STRING
)
RETURNS STRING
LANGUAGE PYTHON
AS
$$

import requests
import json

def udf_send_post_request(url, payload):
try:
# Convert payload from string to JSON
payload_json = json.loads(payload)
response = requests.post(url, json=payload_json)
response.raise_for_status() # Raise an error for bad status codes
return response.text
except requests.exceptions.RequestException as e:
return f"Error: {e}"

$$;

====================  USAGE ===========================

# Set the catalog and schema
spark.sql("USE CATALOG mycatalog")
spark.sql("USE SCHEMA myschema")


# Example usage
url = "myURL"

payload = {
"type": "AdaptiveCard",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.2",
"body": [
{
"type": "TextBlock",
"text": "**TEST EXTERNAL FUNCTION**",
}
]
}
}
]
}

payload_str = json.dumps(payload)

# Create a DataFrame with the URL and payload
df = spark.createDataFrame([(url, payload_str)], ["url", "payload"])


# Use the function in a SQL query
df.createOrReplaceTempView("temp_table")
result = spark.sql(f"""SELECT udf_send_post_request(url, payload) AS response FROM temp_table""")
result.show()

 

Thank You for Any & All Input,

Bob

1 REPLY 1

SQLBob
New Contributor II

This has been dropped in favor of using a function defined internally within a notebook. If anyone has occasion to get a similar process set up - please let me know.

Thanks

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now