63 lines
2.6 KiB
C#
63 lines
2.6 KiB
C#
using IAChat.MSGraph;
|
|
using Microsoft.Bot.Builder;
|
|
using Microsoft.Bot.Builder.Teams;
|
|
using Microsoft.Bot.Schema;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Web;
|
|
|
|
namespace IAChat.Bot;
|
|
|
|
public class ChatIABot : TeamsActivityHandler
|
|
{
|
|
private readonly IHttpClientFactory clientFactory;
|
|
private readonly TokenManager tokenManager;
|
|
|
|
//public ChatIABot(IHttpClientFactory clientFactory, IConfigurationManager configuration)
|
|
public ChatIABot(IServiceProvider serviceProvider)
|
|
{
|
|
this.clientFactory = serviceProvider.GetService<IHttpClientFactory>();
|
|
this.tokenManager = serviceProvider.GetService<TokenManager>();
|
|
}
|
|
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
|
|
{
|
|
string messageText = turnContext.Activity.RemoveRecipientMention()?.Trim();
|
|
var userId = turnContext.Activity.From.Id;
|
|
var userName = turnContext.Activity.From.Name;
|
|
var tenantId = turnContext.Activity.Conversation.TenantId;
|
|
var aadObjectId = turnContext.Activity.From.AadObjectId;
|
|
|
|
var token = await tokenManager.ObterToken(userId);
|
|
var replyText = await GetMessage(token, messageText);
|
|
await turnContext.SendActivityAsync(MessageFactory.Text(replyText), cancellationToken);
|
|
}
|
|
|
|
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
|
|
{
|
|
var welcomeText = "Olá! Eu sou o Ian. Uma inteligência artificial interna da Domvs iT.\nSe perguntar algo sobre o conteúdo do SahePoint da Domvs, eu conseguirei responder.";
|
|
foreach (var member in membersAdded)
|
|
{
|
|
if (member.Id != turnContext.Activity.Recipient.Id)
|
|
{
|
|
await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText), cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task<string> GetMessage(string jwt, string message)
|
|
{
|
|
var httpClient = this.clientFactory.CreateClient();
|
|
var content = "";
|
|
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:5020/Chat?Message={HttpUtility.UrlEncode(message)}"))
|
|
{
|
|
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
|
|
|
|
var responseGet = await httpClient.SendAsync(requestMessage);
|
|
content = responseGet.Content.ReadAsStringAsync().Result;
|
|
}
|
|
|
|
return content;
|
|
}
|
|
}
|
|
|