MSTEAMS AUTOMATE DAILY UPDATE USING PYTHON.

Qayyum Mujib
TheLorry Data, Tech & Product
4 min readFeb 15, 2022

Speed is an important element in the realm of work. Therefore, to help people be fast and consistent in their work is to use automation, because automation can replace your usual tasks, without people realizing it is a robot. Sometimes doing a job over and over again makes a person bored or stressed facing the same thing, so the use of automation can solve the problem.

What will you gain after reading this article?

You will learn how to connect your msteams with python, connect databases for analysis and a hyperlink button.

In general, my task is about to automate daily sale summary update about company sale in msteams, advantage of msteams because we can directly discuss with team members about the sale performance. Therefore, to get sale summary we need to complete some analysis to understand about the performance either the sale increase or decrease. So the process of this task divide into 2 part,

  1. Data analysis, to study either sale increase or decrease.
  2. Send the output to msteams by using python.

Let’s start with part 1, Data analysis.

  1. Import all the required library

For this analysis, the library named as “mysql.connector”. This library help to read data from mysql database.

So we need to install the library first as below ,

pip install mysql-connector-python

after we install the library then we need to import the library to our notebook as below.

import mysql.connector
import pandas as pd
import numpy as np
import datetime
import time
from datetime import date,timedelta
# read data from mysqlmydb = mysql.connector.connect(
host="host port",
user="username",
password="userpassword",
database ="db name",
connect_timeout=10000
)
mycursor = mydb.cursor()sql = """
you can write sql statement here....
example...
select * from driver
where country = 'Malaysia'
"""
df = pd.read_sql(sql, con = mydb)
yesterday = date.today () - timedelta(days=1)
df = df.fillna(0)

The code above show how to read data from database by using python. You can proceed with any data manipulation because the data in the form of data frame therefore easy for you to proceed with deeper analysis.

Okey, so far I wish you guys already clear how to read data from database by using python. After we have the data, what next? how to make the data useful? how to make every one inside organization notice and aware about company performance? The answer is, we need to share with them about the data. How to share with them?

First, you can sent through email, if you want to know about email automation, you can refer my previous article here.

Second, we can sent through msteams. lets go deeper how to sent the data to msteams.

Btw, I just to let you know, we only can send the data into msteams channel, we cannot send to msteams group or personal chat. Therefore, this the limitation that i facing now, if any of you have experience how to send chat from python to msteams group feel free to share the article with me.

Okey, lets start.

First, you need to configure the connector. I use webhook connector for this project.

click on connectors, and find “Incoming Webhook” then select configure. The step as below.

After that, this page will pop up then just fill in the information, after complete click on create.

After click on Create, you will get webhook link, the link is very importance because the link function as to connect with the python. You need to save the link.

so far we already done to set up the webhook connection inside msteams, therefore the next step we need to connect the msteams with python. The step as below.

install the package first.

pip install pymsteams

then,

import pymsteamswebhook = "the link you get when create webhook connection inside msteams"myTeamsMessage = pymsteams.connectorcard(webhook)
myMessageSection = pymsteams.cardsection()
myMessageSection1 = pymsteams.cardsection()
myMessageSection2 = pymsteams.cardsection()
# Section Title
myTeamsMessage.title("Dailies Update B2C ID")
comment_rev = "sale revenue comment"
comment_total = "no of booking" # this information i get from the data analysis, just need some manipulation to get the summary, you can use your creativity to summaries about the daily company performance.
# Activity Elements
myMessageSection.activitySubtitle(comment_rev)
myMessageSection2.activitySubtitle(comment_total)
# Then send the card
myTeamsMessage.addSection(myMessageSection)
myTeamsMessage.addSection(myMessageSection1)
myTeamsMessage.addSection(myMessageSection2)
myTeamsMessage.addLinkButton("Explore More", "https://goole.com")
myTeamsMessage.summary("Test Message")
myTeamsMessage.send()

so when you run the code, you will get something like this inside the webhook.

Tadaaaaa…. finish for automate update daily sale to teams member. When someone click on Explore more it will bring the person to our main dashboard. The purposed for this task to make everyone aware about the company sale performance.

Thank you for your time….

--

--