Automating MS Teams
Tags: python, productivity, tutorial
Published: 2020-09-02 12:40:01



### My Workflow

Most of us are aware that **Teams** is a video conferencing app which allows us to attend/conduct meetings. And because of this pandemic situation, the usage of these video conferencing apps also increased and most of the classes and lectures and conferences are conducted in **Teams** now a days.

Let us see how I have automated **Teams** so that it automatically logs into one's meetings/classes on time.

* An infinite loop keeps checking the current time of the system using `datetime.now()` funtion.
* The Teams app is opened using `os.startfile()`.
* `pyautogui.locateCenterOnScreen()` function locates the center of the first found instance of the image on the screen.
* `pyautogui.moveTo()` moves the cursor to that location.
* `pyautogui.click()` performs a click operation.

### Submission Category: 

[Note]: # (Maintainer Must-Haves, DIY Deployments, Interesting IoT, Phone Friendly, or Wacky Wildcards)
Wacky Wildcards

### Yaml File or Link to Code
{% github https://github.com/Chitturiarunkrishna/msteams %}

### Additional Resources / Info

**Pre-requisites:**
* Microsoft Teams application - Download the exe file from [here](https://www.microsoft.com/en-in/microsoft-365/microsoft-teams/download-app)
* Python
* Pyautogui

**[Pyautogui :](https://pypi.org/project/PyAutoGUI/)**
**PyAutoGUI** is a cross-platform GUI automation Python module. It programmatically controls the mouse & keyboard. It has screen shot features and also **image finding** features.

```python
pip install pyautogui
pip install python-intervals
```


**Note :**
Before using the script make sure that the **Teams** application is closed in such a way that all the teams are displayed as shown in the below figure.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jf9mly0fxrvd6aj94le0.png)

**Import the necessary modules**
```python
import os             
import pyautogui
import intervals as I
import time
from time import sleep
from datetime import datetime
```
**Open Teams**
```python
os.startfile("C:/Users/username/AppData/Local/Microsoft/Teams/current/Teams.exe") 
```
**Next**, take the screenshot of settings button and use the below chunk of code to move the mouse to that button and click it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ir3eabucsf1o9xycdnp0.PNG)
```python
settings = pyautogui.locateCenterOnScreen("settings.PNG") 
pyautogui.moveTo(settings)
pyautogui.click()
time.sleep(2)
```
Now we need to click on **Manage Teams** button to get a list of teams vertically.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/50o2y3pma3re1kin88cf.PNG)

```python
manageteams = pyautogui.locateCenterOnScreen("manageteams.PNG") 
pyautogui.moveTo(manageteams)
pyautogui.click()
time.sleep(2)
```
then we will get the screen like this

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b7q0ed96kt7qek4wves1.png)

Now we run the below code snippet in an infintie loop.

We get the current time and compare it with the **class ending time** . For example if current time is 17:00 and my class is at 17:00 but the meeting has started. If I give my class ending time i.e 18:00 then as soon as meeting starts the scripts automatically clicks on join button with **camera and microphone** turned off.

* **Join button** is given below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y1732u6t32w99oh4n62l.PNG)

* **Camera and microphone** button is given below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6rgmu361lue7t5dyhrb0.PNG)

* Similarily take the screenshot of your class name too.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oswnee6lwyif1817g1ov.PNG)

```python
if now < '18:00':
	Andromeda =pyautogui.locateCenterOnScreen("Andromeda.PNG") 
	pyautogui.moveTo(Andromeda)
	pyautogui.click()
	time.sleep(2)
	join = pyautogui.locateCenterOnScreen("join.PNG") 
	pyautogui.moveTo(join)
	pyautogui.click()
	time.sleep(2)
	audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
	pyautogui.moveTo(audiooff)
	pyautogui.click()
	time.sleep(2)
```

Similarily you can do it for **n** number of classes. 
In the below code I have done it for 2 classes.

```python
import os             
import pyautogui
import intervals as I
import time
from time import sleep
from datetime import datetime


try:
	# open MS Teams application
	os.startfile("C:/Users/username/AppData/Local/Microsoft/Teams/current/Teams.exe") 
	sleep(2)
	# settings
	settings = pyautogui.locateCenterOnScreen("settings.PNG") 
	pyautogui.moveTo(settings)
	pyautogui.click()
	time.sleep(2)
	# manageteams.PNG
	manageteams = pyautogui.locateCenterOnScreen("manageteams.PNG") 
	pyautogui.moveTo(manageteams)
	pyautogui.click()
	time.sleep(2)
except Exception as e:
	print(e)

while True:
	#andromeda
	now = datetime.now().strftime("%H:%M")
	if now < '18:00':
		Andromeda = pyautogui.locateCenterOnScreen("Andromeda.PNG") 
		pyautogui.moveTo(Andromeda)
		pyautogui.click()
		time.sleep(2)
		join = pyautogui.locateCenterOnScreen("join.PNG") 
		pyautogui.moveTo(join)
		pyautogui.click()
		time.sleep(2)
		audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
		pyautogui.moveTo(audiooff)
		pyautogui.click()
		time.sleep(2)
	elif now <'17:00': 
		Automation = pyautogui.locateCenterOnScreen("Automation.PNG") 
		pyautogui.moveTo(Automation)
		pyautogui.click()
		time.sleep(2)
		join = pyautogui.locateCenterOnScreen("join.PNG") 
		pyautogui.moveTo(join)
		pyautogui.click()
		time.sleep(2)
		audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
		pyautogui.moveTo(audiooff)
		pyautogui.click()
		time.sleep(2)
```
Suppose if you want to set the time interval and check for meeting to start and then join please see this
```python
while True:
	#andromeda
	k = datetime.now().strftime("%H")
	now = int(k)
	if now in I.closed(16, 17):
		andromeda = pyautogui.locateCenterOnScreen("Andromeda.PNG") 
		pyautogui.moveTo(andromeda)
		pyautogui.click()
		time.sleep(2)
		join = pyautogui.locateCenterOnScreen("join.PNG") 
		pyautogui.moveTo(join)
		pyautogui.click()
		time.sleep(2)
		audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
		pyautogui.moveTo(audiooff)
		pyautogui.click()
		time.sleep(2)
		if now == 17:
			cut = pyautogui.locateCenterOnScreen("cut.PNG") 
			pyautogui.moveTo(cut)
			pyautogui.click()
			time.sleep(2)
			dismiss = pyautogui.locateCenterOnScreen("dismiss.PNG") 
			pyautogui.moveTo(dismiss)
			pyautogui.click()
			time.sleep(2)
			back = pyautogui.locateCenterOnScreen("back.PNG") 
			pyautogui.moveTo(back)
			pyautogui.click()
			time.sleep(2)
```
Then the entire code will be as below:
```python
import os             
import pyautogui
import intervals as I
import time
from time import sleep
from datetime import datetime


try:
	# open MS Teams application
	os.startfile("C:/Users/username/AppData/Local/Microsoft/Teams/current/Teams.exe") 
	sleep(2)
	# settings
	settings = pyautogui.locateCenterOnScreen("settings.PNG") 
	pyautogui.moveTo(settings)
	pyautogui.click()
	time.sleep(2)
	# manageteams.PNG
	manageteams = pyautogui.locateCenterOnScreen("manageteams.PNG") 
	pyautogui.moveTo(manageteams)
	pyautogui.click()
	time.sleep(2)
except Exception as e:
	print(e)

while True:
	#andromeda
	k = datetime.now().strftime("%H")
	now = int(k)
	if now in I.closed(16, 17):
		andromeda = pyautogui.locateCenterOnScreen("Andromeda.PNG") 
		pyautogui.moveTo(andromeda)
		pyautogui.click()
		time.sleep(2)
		join = pyautogui.locateCenterOnScreen("join.PNG") 
		pyautogui.moveTo(join)
		pyautogui.click()
		time.sleep(2)
		audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
		pyautogui.moveTo(audiooff)
		pyautogui.click()
		time.sleep(2)
		if now == 17:
			cut = pyautogui.locateCenterOnScreen("cut.PNG") 
			pyautogui.moveTo(cut)
			pyautogui.click()
			time.sleep(2)
			dismiss = pyautogui.locateCenterOnScreen("dismiss.PNG") 
			pyautogui.moveTo(dismiss)
			pyautogui.click()
			time.sleep(2)
			back = pyautogui.locateCenterOnScreen("back.PNG") 
			pyautogui.moveTo(back)
			pyautogui.click()
			time.sleep(2)
	elif now in I.closed(17, 18):
		automation = pyautogui.locateCenterOnScreen("automation.PNG") 
		pyautogui.moveTo(automation)
		pyautogui.click()
		time.sleep(2)
		join = pyautogui.locateCenterOnScreen("join.PNG") 
		pyautogui.moveTo(join)
		pyautogui.click()
		time.sleep(2)
		audiooff = pyautogui.locateCenterOnScreen("audiooff.PNG") 
		pyautogui.moveTo(audiooff)
		pyautogui.click()
		time.sleep(2)
		if now == 18:
			cut = pyautogui.locateCenterOnScreen("cut.PNG") 
			pyautogui.moveTo(cut)
			pyautogui.click()
			time.sleep(2)
			dismiss = pyautogui.locateCenterOnScreen("dismiss.PNG") 
			pyautogui.moveTo(dismiss)
			pyautogui.click()
			time.sleep(2)
			back = pyautogui.locateCenterOnScreen("back.PNG") 
			pyautogui.moveTo(back)
			pyautogui.click()
			time.sleep(2)
```

I have uploaded the entire code and all the required files in my repository. Please drop a star if you like it.