# Microsoft Teams

Microsoft Teams messages to channels, people and groups through self.microsoft.teams.send.

Zato integrates with Microsoft Teams, letting you send messages to channels, people and groups from your Python services through `self.microsoft.teams.send`.

The connections are send-only - they are for delivering notifications, alerts and updates from your integrations to the people who need them.

A Teams connection is a [Microsoft 365](https://zato.io/tutorials/microsoft/365.html) connection that talks to the Graph API - the same Azure app registration, credentials and authentication, only the screens and the service API differ.

## Getting your Microsoft app credentials {#getting-your-microsoft-app-credentials}

The connection uses the same credentials as any other Microsoft 365 one - a tenant ID, a client ID and a client secret of an app registered in your organization. The [Microsoft 365 tutorial](https://zato.io/tutorials/microsoft/365.html) covers how to obtain them.

In addition, the app registration needs Graph application permissions to work with Teams, granted with admin consent:

- `Team.ReadBasic.All` and `Channel.ReadBasic.All` - to resolve team and channel names to their IDs
- `ChannelMessage.Send` - to post messages to channels
- `Chat.ReadWrite.All` - to post messages to chats with people and groups

## Configuring Zato {#configuring-zato}

1. Go to **Connections** → **Chat** → **Microsoft Teams**
2. Click **Create a new connection**
3. Enter a name for the connection, along with the client ID, tenant ID and scopes, and click OK
4. Click **Change secret** on the connection's page and enter the client secret

The same screens are also reachable through **Cloud** → **Microsoft** → **Teams** and **Vendors** → **Microsoft** → **Teams**.

The same connections can also be created with [enmasse](https://zato.io/docs/admin/enmasse-reference.html#microsoft-teams), using the `microsoft_teams` key:

```yaml
microsoft_teams:
  - name: My Teams
    client_id: 45678901-4567-4567-4567-4567890abcde
    secret_value: Zato_Enmasse_Env.MicrosoftTeamsSecretValue
    scopes: https://graph.microsoft.com/.default
    tenant_id: 87654321-7654-7654-7654-fedcba987654
```

Use **Ping** on the connection's page to confirm that the credentials work.

## Sending messages {#sending-messages}

Messages are sent with `self.microsoft.teams.send`, giving it the connection's name, the target and the text:

```python
from zato.server.service import Service

class NotifyOnboardingComplete(Service):

    def handle(self):
        self.microsoft.teams.send('My Teams', to='My Team/General', text='All accounts for Jane Doe are ready')
```

The target can be:

- A channel, given as `Team name/Channel name` - the names are resolved to their Graph IDs automatically
- A chat with a person or a group, given as its chat ID, e.g. `19:b8577894a63548969c5c92bb9c80c5e1@thread.v2`

## Rich text {#rich-text}

The text is HTML, which is how Teams messages deliver rich content:

```python
from zato.server.service import Service

class NotifyDeploymentDone(Service):

    def handle(self):

        text = """
        <b>Deployment finished</b>
        <ul>
            <li>Environment: production</li>
            <li>Status: <i>all checks passed</i></li>
        </ul>
        """

        self.microsoft.teams.send('My Teams', to='My Team/Deployments', text=text)
```

## More than sending {#more-than-sending}

The client behind each connection exposes the full Microsoft 365 client, should you need more than sending messages:

```python
from zato.server.service import Service

class ListTeams(Service):

    def handle(self):

        # The underlying client - its .impl attribute is the same Microsoft 365 client
        # that self.microsoft.cloud connections use.
        client = self.microsoft.teams['My Teams']

        # Use the Graph API directly
        response = client.impl.con.get('https://graph.microsoft.com/v1.0/teams')

        for team in response.json()['value']:
            self.logger.info('Team: %s', team['displayName'])
```

## Learn more {#learn-more}

- [Development documentation](https://zato.io/docs/dev/) - The complete reference for writing services
- [Requests and responses](https://zato.io/docs/dev/request-response/) - What a service receives, what it returns and how both are structured
- [Integration examples](https://zato.io/docs/dev/examples/) - Ready-made code for the systems you are likely to connect to
- [IDE and debugging](https://zato.io/docs/dev/ide/) - Write services in the Dashboard or in your own editor
- [Data models](https://zato.io/docs/dev/model/) - Declare inputs and outputs and have them validated for you
- [In-depth API tutorial](https://zato.io/tutorials/main/01.html) - The full platform tutorial, from installation to production patterns
