最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

botframework - Microsoft Teams Bot: send a message to a Teams User via REST API calls - Stack Overflow

programmeradmin4浏览0评论

First off, I'm new to Microsoft Teams (and the larger office/365/azure ecosystem), and have been a bit overwhelmed at the complexity needed to perform what seemed like a simple action. I have a webapp that integrates with apps like Slack to send individual reminders to users. We prefer individual "one-to-one" messages over spamming a whole team, because we only want to remind users that need reminding, and we want to include a link they can click that is specific to that user.

I've been tasked with making this same idea work in MS Teams. I've created an Azure App, and added a bot, and I can freely hit endpoints on the Microsoft Graph API, but I haven't found the right incantation to create a chat and send a message from my bot to a User via Graph. So now I'm trying to use the botframework, but with the hopes of boiling it down to a series of REST API calls, and not to require an always on bot running in the cloud somewhere. We have no plans for the user to message the bot, or for the bot to reply to the user in any way. Instead, I just have a cron job that will wake up, generate a list of users and links needed to be sent, and then send them. I want to be able to send my messages to Teams users in that moment, and detect any errors as each message is sent.

I've found that with what Microsoft calls a "serviceUrl", I can POST to {surviceUrl}/v3/conversations to create a chat, and then POST to {surviceUrl}/v3/conversations/{chatId}/activities to send a message. The problem is that I can't figure out how to get the correct serviceUrl automatically. By hard coding /, I can get it to work for a test tenant, but I'm led to believe that the serviceUrl is different for different tenants, and MS reserves the right to change them at any time.

So how do I get the correct serviceUrl? Or am I barking up the wrong tree, and I need to take a different approach?

First off, I'm new to Microsoft Teams (and the larger office/365/azure ecosystem), and have been a bit overwhelmed at the complexity needed to perform what seemed like a simple action. I have a webapp that integrates with apps like Slack to send individual reminders to users. We prefer individual "one-to-one" messages over spamming a whole team, because we only want to remind users that need reminding, and we want to include a link they can click that is specific to that user.

I've been tasked with making this same idea work in MS Teams. I've created an Azure App, and added a bot, and I can freely hit endpoints on the Microsoft Graph API, but I haven't found the right incantation to create a chat and send a message from my bot to a User via Graph. So now I'm trying to use the botframework, but with the hopes of boiling it down to a series of REST API calls, and not to require an always on bot running in the cloud somewhere. We have no plans for the user to message the bot, or for the bot to reply to the user in any way. Instead, I just have a cron job that will wake up, generate a list of users and links needed to be sent, and then send them. I want to be able to send my messages to Teams users in that moment, and detect any errors as each message is sent.

I've found that with what Microsoft calls a "serviceUrl", I can POST to {surviceUrl}/v3/conversations to create a chat, and then POST to {surviceUrl}/v3/conversations/{chatId}/activities to send a message. The problem is that I can't figure out how to get the correct serviceUrl automatically. By hard coding https://smba.trafficmanager/amer/, I can get it to work for a test tenant, but I'm led to believe that the serviceUrl is different for different tenants, and MS reserves the right to change them at any time.

So how do I get the correct serviceUrl? Or am I barking up the wrong tree, and I need to take a different approach?

Share Improve this question asked Mar 17 at 18:25 brakeleybrakeley 3021 silver badge7 bronze badges 6
  • You may use the code below to retrieve the latest serviceUrl from turnContext.Activity.ServiceUrl.Please note that service URLs are unique to each region, not the tenant. You can cache the team and user details when the bot is added to a team during the conversation update bot event, and use them later to send messages. Even if the service URL changes for a user and becomes outdated, it’s still fine to reuse it, though it might not be optimal due to a different traffic manager associated with a different region. – Sayali-MSFT Commented Mar 18 at 9:27
  • 1 For Reference-1.github/OfficeDev/Microsoft-Teams-Samples/blob/… 2.learn.microsoft/en-us/microsoftteams/platform/bots/how-to/… – Sayali-MSFT Commented Mar 18 at 9:28
  • @Sayali-MSFT So that second link seems to be saying that I can always use the https://smba.trafficmanager/teams/ serviceUrl, but that if I receive a different serviceUrl, I should cache that and use it instead. If I can always use that serviceUrl, but it just might be slower than using the "correct" serviceUrl, is anything stopping me from just ALWAYS using that /teams/ url in my code? I don't care how quickly these messages are sent. They are sent once a month, and I just need to know they got sent, not for them to be sent instantly. – brakeley Commented Mar 18 at 19:22
  • Also, to reiterate: we are hoping to just make REST requests, without needing to expose any webhooks. I don't know how to get a turnContext.Activity.* in this context. Is that a non-starter, or if all we're doing is sending notices to a short list of users once a month, can we get by with just hitting up https://smba.trafficmanager/teams/v3/conversations to make/get a conversation chatId, and then .../v3/conversations/{chatId}/activities to send our message? – brakeley Commented Mar 18 at 19:55
  • In scenarios where you're not using bot frameworks and not dealing with real-time interactions (i.e., receiving messages and responding), obtaining the turnContext.Activity.* is not necessarily required. If your goal is purely to send messages at scheduled intervals, you can proceed with the REST API calls as you've indicated. Use the endpoint smba.trafficmanager/teams/v3/conversations to create or retrieve the chatId for users. – Sayali-MSFT Commented Mar 28 at 7:18
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Thanks @Sayali-MSFT for the answer, which is this:

In scenarios where you're not using bot frameworks and not dealing with real-time interactions (i.e., receiving messages and responding), obtaining the turnContext.Activity.* is not necessarily required. If your goal is purely to send messages at scheduled intervals, you can proceed with the REST API calls as you've indicated. Use the endpoint smba.trafficmanager/teams/v3/conversations to create or retrieve the chatId for users.

– Sayali-MSFT

发布评论

评论列表(0)

  1. 暂无评论