有些朋友問到,如果想要開發一個bot針對于Teams的某些租戶,如何做?實際上微軟的Teams的SDK早就提供了類似的功能。
如果你使用的是Javascript/Node.JS開發,使用session.message.sourceEvent.tenant.id
就可以知道當前消息來自于哪個租戶。
技術人員,理論知識不如直接上代碼,如下Javascript實例代碼讓大家更加容易理解一些:
var bot = new builder.UniversalBot(connector);bot.use({botbuilder: function(session, next) {var currentMsgTenant = typeof(session.message.sourceEvent.tenant) !== "undefined"? session.message.sourceEvent.tenant.id : null;....
如果你使用的是C#,那恭喜你了,SDK直接就有現成的對租戶的過濾器
using Microsoft.Bot.Connector.Teams;namespace Teams.Sample.Controllers
{[BotAuthentication, TenantFilter]public class MessagesController : ApiController{[HttpPost]public async Task<HttpResponseMessage> Post([FromBody] Activity activity)
然后只要在你的web.config里進行簡單的配置就可以了
<configuration><appSettings><!--other settings--><add key="AllowedTenants" value="*TenantId1,TenantId2,...*"/>
如果你走在技術前沿,使用了dotnet core 那很不幸,在我寫這篇文章的時候dotnet core teams SDK還不支持TenantFilter。不過慶幸的是SDK是開源的,https://github.com/OfficeDev/BotBuilder-MicrosoftTeams/blob/master/CSharp/Library/Microsoft.Bot.Connector.Teams.NetFramework/TenantFilterAttribute.cs
代碼面前毫無秘密。
而且按照Teams的發展速度,應該很快就能有asp.net core的版本了,大家拭目以待!