将Microsoft Bot框架连接到Ms Teams(Ms Teams中未显示主动消息)

时间:2019-09-05 04:46:23

标签: c# frameworks bots

我无法从Microsoft BOT V4在Microsoft Teams中显示主动消息。 该漫游器当前已部署在Azure中,并且可以连接到Microsoft团队渠道。 正在显示其他消息。但是,未显示主动消息。在网络聊天中效果很好。


// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.


using System;
using System.Collections.Concurrent;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using EchoBotnivin.services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
using ATR_AQ_2;
using EchoBotnivin.domain;
using System.Collections.Generic;

namespace EchoBotnivin.Controllers
{
   [Route("api/notify")]
   [ApiController]
   public class NotifyController : ControllerBase
   {
       public static bool notifWhenStateChanges = false;
       public static bool notifWhenAboutToKo = false;
       public static bool notifNewTickets = true;
       public static bool leaaccessrequest = false;
       public static bool notifyCommentsForm = false;
       string tokenAuth;
       int j = 0;
       //private string ServiceUrl;
       private readonly IBotFrameworkHttpAdapter _adapter;
       private readonly string _appId;
       private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;

       public NotifyController(IBotFrameworkHttpAdapter adapter, IConfiguration configuration, ConcurrentDictionary<string, ConversationReference> conversationReferences)
       {
           _adapter = adapter;
           _conversationReferences = conversationReferences;
           _appId = configuration["MicrosoftAppId"];


           // If the channel is the Emulator, and authentication is not in use,
           // the AppId will be null.  We generate a random AppId for this case only.
           // This is not required for production, since the AppId will have a value.
           if (string.IsNullOrEmpty(_appId))
           {
               _appId = Guid.NewGuid().ToString(); //if no AppId, use a random Guid
           }
       }

       public async Task<IActionResult> Get()
       {
           foreach (var conversationReference in _conversationReferences.Values)
           {
               if (notifNewTickets == true)
                   await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));

           }
           // Let the caller know proactive messages have been sent
           return new ContentResult()
           {
               Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
               ContentType = "text/html",
               StatusCode = (int)HttpStatusCode.OK,
           };

       }


       private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
       {
           /* ServiceUrl = "https://teams.microsoft.com";
            var teamConversationData = turnContext.Activity.GetChannelData<TeamsChannelData>();
            var connectorClient = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), _credentialProvider.AppId, _credentialProvider.Password);
            var userId = "adgsgjjygghghdfgsdfsf";
            var tenantId = "wrewrerweercxgdfdggjdgssd";
            var parameters = new ConversationParameters
            {
                Members = new[] { new ChannelAccount(userId) },
                ChannelData = new TeamsChannelData
                {
                    Tenant = new TenantInfo(tenantId),
                },
            };

            var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
            var message = Activity.CreateMessageActivity();
            message.Text = "This is a proactive message.";
            await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)message);
           */
           var token = new Connector();
           tokenAuth = token.getToken();

           var token23 = new Connector();
           dynamic st12 = new servicenowdata();
           st12 = token23.getJobs(tokenAuth);

          Int32 length = st12.Count;
           if (length > 0)
           {
               await turnContext.SendActivityAsync(MessageFactory.Text($"Hello!" + Environment.NewLine + $"Trust you are well" + Environment.NewLine + $"Hope that you are having a good day" + Environment.NewLine + $"We are contacting you concerning lea access requests" + Environment.NewLine + $"Could you please review the following tasks if any and add the required information!"), cancellationToken);
               //await turnContext.SendActivityAsync(MessageFactory.Text($"Please type ok to continue!"), cancellationToken);
               await turnContext.SendActivityAsync(MessageFactory.Text("Could you please click on the below button to continue?"));

               var card = new HeroCard
               {
                   //Text = "Could you please click on the below button to continue?",
                   Buttons = new List<CardAction>
                       {
                           new CardAction(ActionTypes.ImBack, title: "lea access request", value: "lea access request"),
                       },
               };
               var reply = MessageFactory.Attachment(card.ToAttachment());
               await turnContext.SendActivityAsync(reply, cancellationToken);

               //The above tickets have just been assigned to our group could you please check. Have a nice day ahead
           }



        }


   }

}

0 个答案:

没有答案