Skip to content

Instantly share code, notes, and snippets.

@bobpeers
Last active June 14, 2022 10:22
Show Gist options
  • Save bobpeers/67474756c182f2a9a9068e380812cd24 to your computer and use it in GitHub Desktop.
Save bobpeers/67474756c182f2a9a9068e380812cd24 to your computer and use it in GitHub Desktop.
Sample to return order information from Azure using Teams Bot (outgoing webhook)
import json
import logging
from nis import match
import os
import re
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.engine import URL
import azure.functions as func
db: str = os.getenv('SQLAZURECONNSTR_AZURE_SQL_DATABASE')
err: str = ''
def azure_db(connection_string):
connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
engine = create_engine(connection_url)
return engine.connect()
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
try:
req_body = req.get_json()
bdy = req.get_body().decode()
except ValueError:
pass
else:
message = req_body.get('text')
bot = req_body.get('name')
order = re.search('SO\d{5}', message)
if order:
order = order.group(0)
azure_engine = azure_db(db)
sql: str = f'Select sum([Outstanding Amount]) from dbo.view_sales_open where [Order No_] =\'{order}\''
data = pd.read_sql(sql, azure_engine)
azure_engine.close()
if not order:
return func.HttpResponse(
json.dumps({
"type": "message",
"text": "Sorry, you didn't send a valid order number (for example SO12345)"}),
status_code=500
)
else:
return func.HttpResponse(
json.dumps({
"type": "message",
"text": f'Order {order} has {int(data.iat[0,0])} items not shipped'}),
status_code=200
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment