Send SQL Results to MS Teams Using Python

Step 1:

Open teams and go to create new team page

Step 2:

Click on Join or Create a team

Step 3:

Click on create a team and select build a team from scratch

Step 4:

Select kind of team

Step 5:

Provide team name as shown in bellow

Click on create

Step 6:

Add member if required


Step 7:

Click on 3 dots (...) and select connectors as shown in bellow

Step 8:

Select Incoming Webhook and configured as shown in bellow


Setup incoming webhook name as shown and click on create

Copy webhook url and put into mentioned python script


Step 9:

Python script

import pymsteams
import pandas as pd
import numpy as np
import pyodbc

server = 'ServerName'
database = 'msdb'
username = 'LoginName'
password = 'Password'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

cursor = cnxn.cursor()
query = "select top 10 [name],[type_desc],create_date from msdb.sys.tables"

df = pd.read_sql(query, cnxn)

#def sleeve(df):
# df.to_html()

myTeamsMessage = pymsteams.connectorcard("Webhook URL")
myTeamsMessage.title("Top 10 System Tables in msdb database")
myTeamsMessage.text(df.to_html(index=False))
myTeamsMessage.send()

Results looks like bellow



Comments

Popular Posts

Failed to execute the package or element. Build errors were encountered

Restore of database 'DataBase_Name' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

Cannot convert "Column" between a unicode and a non-unicode string data types in SSIS