Skip to content

Instantly share code, notes, and snippets.

@arafattehsin
Created October 30, 2019 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arafattehsin/de467bdf5073183ae9913f222b266239 to your computer and use it in GitHub Desktop.
Save arafattehsin/de467bdf5073183ae9913f222b266239 to your computer and use it in GitHub Desktop.
Excerpt from the main Bot class; for complete code, please refer to CognitiveRocket repo.
private async Task ProcessVABotAsync(ITurnContext<Microsoft.Bot.Schema.IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var token = await _botServices.VABotService.GetTokenAsync();
using (var directLineClient = new DirectLineClient(token))
{
var conversation = await directLineClient.Conversations.StartConversationAsync();
var conversationtId = conversation.ConversationId;
var response = await directLineClient.Conversations.PostActivityAsync(conversationtId, new Microsoft.Bot.Connector.DirectLine.Activity()
{
Type = Microsoft.Bot.Connector.DirectLine.ActivityTypes.Message,
From = new Microsoft.Bot.Connector.DirectLine.ChannelAccount { Id = "userId", Name = "userName" },
Text = turnContext.Activity.Text,
ChannelData = JObject.FromObject(_botServices.VABotService.ChannelData),
TextFormat = "plain",
Locale = "en-Us",
});
Thread.Sleep(4000);
var activities = await GetActivitiesAsync(directLineClient, conversationtId, _botServices.VABotService.BotName);
var activity = turnContext.Activity as Microsoft.Bot.Schema.Activity;
await turnContext.SendActivitiesAsync(
activities
.Select(message =>
{
var reply = activity.CreateReply(message.Text);
reply.Attachments = message?.Attachments?.Select(a => new Microsoft.Bot.Schema.Attachment()
{
Content = a.Content,
ContentType = a.ContentType,
ContentUrl = a.ContentUrl
}).ToList();
reply.SuggestedActions = new Microsoft.Bot.Schema.SuggestedActions()
{
Actions = message?.SuggestedActions?.Actions?.Select(a => new Microsoft.Bot.Schema.CardAction()
{
Title = a.Title,
Value = a.Value,
Type = a.Type,
Image = a.Image
}).ToList(),
};
return reply;
})
.ToArray());
}
}
private async Task<List<Microsoft.Bot.Connector.DirectLine.Activity>> GetActivitiesAsync(DirectLineClient directLineClient, string conversationtId, string botName)
{
ActivitySet response = null;
List<Microsoft.Bot.Connector.DirectLine.Activity> result = new List<Microsoft.Bot.Connector.DirectLine.Activity>();
string watermark = null;
do
{
response = await directLineClient.Conversations.GetActivitiesAsync(conversationtId, watermark);
watermark = response.Watermark;
result = response?.Activities?.Where(x =>
x.Type == Microsoft.Bot.Connector.DirectLine.ActivityTypes.Message &&
string.Equals(x.From.Name, botName, StringComparison.Ordinal)).ToList();
if (result != null && result.Any())
{ return result; }
Thread.Sleep(1000);
} while (response.Activities.Any());
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment