Skip to main content

Proactive Messaging

In Sending Messages, you were shown how to respond to an event when it happens. However, there are times when you want to send a message to the user without them sending a message first. This is called proactive messaging. You can do this by using the send method on your app instance. This approach is useful for sending notifications or reminders to the user.

The main thing to note is that you need to have the conversation_id of the chat or channel that you want to send the message to. It's a good idea to store this value somewhere from an activity handler so that you can use it for proactive messaging later.

from microsoft_teams.api import InstalledActivity, MessageActivityInput
from microsoft_teams.apps import ActivityContext
# ...

# This would be some persistent storage
storage = dict[str, str]()

# Installation is just one place to get the conversation_id. All activities have this field as well.
@app.on_install_add
async def handle_install_add(ctx: ActivityContext[InstalledActivity]):
# Save the conversation_id
storage[ctx.activity.from_.aad_object_id] = ctx.activity.conversation.id
await ctx.send("Hi! I am going to remind you to say something to me soon!")
# This queues up the proactive notifaction to be sent in 1 minute
notication_queue.add_reminder(ctx.activity.from_.aad_object_id, send_proactive_notification, 60000)

Then, when you want to send a proactive message, you can retrieve the conversation_id from storage and use it to send the message.

from microsoft_teams.api import MessageActivityInput
# ...

async def send_proactive_notification(user_id: str):
conversation_id = storage.get(user_id, "")
if not conversation_id:
return
activity = MessageActivityInput(text="Hey! It's been a while. How are you?")
await app.send(conversation_id, activity)
tip

In this example, you see how to get the conversation_id using one of the activity handlers. This is a good place to store the conversation id, but you can also do this in other places like when the user installs the app or when they sign in. The important thing is that you have the conversation id stored somewhere so you can use it later.

Targeted Proactive Messages​

Targeted messages, also known as ephemeral messages, are delivered to a specific user in a shared conversation. From a single user's perspective, they appear as regular inline messages in a conversation. Other participants won't see these messages.

When sending targeted messages proactively, you must explicitly specify the recipient account.

from microsoft_teams.api import MessageActivityInput, Account

# When sending proactively, you must provide an explicit recipient account
async def send_targeted_notification(conversation_id: str, recipient: Account):
await app.send(
conversation_id,
MessageActivityInput(text="This is a private notification just for you!")
.with_recipient(recipient, is_targeted=True)
)

Proactive Threading​

The SDK sends messages into reply chains (threads) in channels and 1:1 chats. Whether a thread is shown depends on the service and the Teams client. The SDK sends a response from a handler through the reply chain route only when channelData.replyChain.id is present on the inbound activity. With no replyChain, the SDK skips that route and sends to the inbound conversation. In a channel, the suffix of that conversation id can still keep the response in the thread.

In this guide, top-level means a message in the main chat or channel conversation, and in-thread means a message in a reply chain (thread).

The app methods take explicit ids, so they work outside a handler. Given the id of the chat or channel (channelData.chat.id or channelData.channel.id), in 1:1 chats and channels they place the message as follows:

Method1:1 chatChannel
app.send()Top-levelTop-level
app.reply()In-threadIn-thread

To proactively send a message as a reply in a thread, use app.reply() with the id of the chat or channel and the id of the top-level message of the thread. Take the ids from channelData on the inbound activity:

  • Chat id: channelData.chat.id, in a 1:1 chat.
  • Channel id: channelData.channel.id, in a channel.
  • Reply chain id: channelData.replyChain.id, the id of the top-level message of the thread.
    • A replyChain.id that matches the message's own activity.id indicates a top-level post. A different replyChain.id indicates the message is in-thread.
    • If replyChain is absent, there is no reply chain id to take from the inbound activity, so pass an id you stored yourself. The SDK does not take it from the conversation id.
# chat_or_channel_id is channel_data.chat.id or channel_data.channel.id
# reply_chain_id is channel_data.reply_chain.id
await app.reply(chat_or_channel_id, reply_chain_id, "Thread update!")

To send a new top-level (non-threaded) message instead, send to the id of the chat or channel with app.send().

# Send a new top-level message to the chat or channel
await app.send(chat_or_channel_id, "Hello!")

If the reply chain call fails, app.reply() throws the error to your code. It does not fall back to another route.

Inside a handler, a response goes to the service_url of the inbound activity. app.send() and app.reply() go to the service_url argument when you pass one, and otherwise to the app's service URL: the service_url app option, else the SERVICE_URL environment variable, else https://smba.trafficmanager.net/teams.

A service URL without its cloud segment can be accepted and still post the message flat, outside the thread. Store the full service URL of the inbound activity, and pass or configure it for proactive calls, instead of rebuilding or trimming it.

The API client's reply operation, api.conversations.reply_to_activity(), is a plain send to the conversation it is given. It posts to the activities route of that conversation and does not place the message in a reply chain. To call the reply chain route directly, use api.conversations.create_reply_chain_activity(), which is experimental. It takes the chat or channel id and the reply chain id, and has no fallback.

If you stored a conversation id from a channel message, it ends in ;messageid=<root post id>. That form identifies the thread, not the chat or channel, and the reply chain endpoint rejects it. Store channelData.chat.id or channelData.channel.id and channelData.replyChain.id instead.

A proactive call has no inbound activity to take an identity from. When the recipient is an agentic user, pass its identity from the inbound activity's recipient. Without it, the call uses the app's own credentials instead of the agentic user's. An agentic app cannot get an app-only token, so the call fails with an authentication error (AADSTS82001) and nothing is sent.

# agentic_identity is None unless the recipient is an agentic user
await app.reply(
chat_or_channel_id,
reply_chain_id,
"Thread update!",
agentic_identity=ctx.activity.recipient.agentic_identity,
)

For reactive threading (within a handler), see Threading.