How I almost automated sending messages to a team chat

sukalpo mitra
4 min readJul 31, 2022

Some time back, a colleague asked me if we could send messages to Microsoft Teams from a shell script. I had no idea but based on my experience with Slack and Telegram, I told him it should be possible.

Later that day, I was still curious and started researching a bit. I knew the best way should be through APIs.

After some fundamental research, I landed on the Graph Explorer page.

It is kind of a sandbox for Microsoft Graph APIs. The first thing I had to do was to Sign in as most of the APIs are not accessible anonymously as most of them need an access token.

Once signed in, I quickly browsed to Create Chat API under the Microsoft Teams Category for Sample Queries.

The “create chat” API creates a one-on-one chat between two members. If a one-on-one chat exists, this operation will return to the existing discussion and not create a new one.

To access the API, you need the access token and need to consent to some permissions as below:-

Now in the sample request body, we provide the id of the person who wants to chat (which is your email id) and the email id of the person with whom you want to chat like below:-

Once we do this and run the Graph Query, it will return a response payload that will hold an id attribute. The value of this id is the id of the chat to which we now should send a message.

Once you get this id, keep it with you. Now browse the send channel message API and consent to the required permissions.

Now, this API is to send a message to a channel. But we want to send it to a chat. Don’t sweat! We need to change the URL to “https://graph.microsoft.com/v1.0/chats/{chat-id}/messages” where {chat-id} needs to be replaced with the id of the chat that we found in the previous API.

Update the request payload with the message you want to send like so:-

{
"body": {
"content": "Hello world"
}
}

And voila, you just sent a Teams message using Microsoft’s Graph API!

Easy right? But now comes the actual problem. If you remember, we got the Access Token by Signing in to the graph API explorer. How do we get this access token programmatically?

So yes, there is a way to get the Access Token through an API, but for that, we need to register ourselves as an app under Azure Active Directory.

But my happiness was short-lived. The documentation for sending chat messages API clearly states that an application does not have permission to send chat messages.

We could not find any other way to get the access token. The access token has 24 hr expiration. For our automation, it was OK to copy the access token from the graph explorer once a day and keep it in a file.

But if it's ok to send a message to a channel, we can use an incoming webhook.

To use an incoming webhook, you can follow the directions here.

Once the webhook is configured, then you can send a message in the format of a card to the channel by sending a POST message to the URL with the following request body:-

{
"@type": "MessageCard",
"summary": "Channel Message",
"sections": [
{
"activityTitle": "Sent Channel Message",
"facts": [
{
"name": "Send For",
"value": "sukalpo.mitra"
},
{
"name": "Message",
"value": "I know what you did last summer!!!"
}
]
}
]
}

And that’s my saga of sending messages to Teams' chats. I hope this helps someone who is about to start the same journey!

Bye for now!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet