Skip to content

Instantly share code, notes, and snippets.

@arafattehsin
Created October 30, 2019 06:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arafattehsin/197f7be86eefa0617dcb903c596cb450 to your computer and use it in GitHub Desktop.
This is core class to connect Dynamics 365 Virtual Agent for Customer Service with Microsoft Bot Framework
public class VABotService
{
private readonly HttpClient _httpClient;
public VABotService(VABotEndpoint endpoint, string botName)
{
Endpoint = endpoint;
BotName = botName;
ChannelData = new VABotChannelData(endpoint.BotId, endpoint.TenantId);
_httpClient = new HttpClient();
}
public string BotName { get; }
public VABotChannelData ChannelData { get; }
public VABotEndpoint Endpoint { get; }
public async Task<string> GetTokenAsync()
{
var httpRequest = new HttpRequestMessage();
httpRequest.Method = new HttpMethod("GET");
httpRequest.RequestUri = Endpoint.TokenUrl;
var response = await _httpClient.SendAsync(httpRequest);
var responseStr = await response.Content.ReadAsStringAsync();
return SafeJsonConvert.DeserializeObject<DirectLineToken>(responseStr).token;
}
}
public class VABotChannelData
{
public VABotChannelData(string botId, string tenantId)
{
DynamicsBotId = botId;
DynamicsTenantId = tenantId;
}
// DO NOT CHANGE property name
[JsonProperty("cci_bot_id")]
public string DynamicsBotId { get; }
// DO NOT CHANGE property name
[JsonProperty("cci_tenant_id")]
public string DynamicsTenantId { get; }
}
public class VABotEndpoint
{
public VABotEndpoint(string botId, string tenantId, string tokenEndPoint)
{
BotId = botId;
TenantId = tenantId;
UriBuilder uriBuilder = new UriBuilder(tokenEndPoint);
uriBuilder.Query = $"botId={BotId}&tenantId={TenantId}";
TokenUrl = uriBuilder.Uri;
}
public string BotId { get; }
public string TenantId { get; }
public Uri TokenUrl { get; }
}
public class DirectLineToken
{
public string token { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment