Skip to content

Instantly share code, notes, and snippets.

@chivandikwa
Created June 8, 2019 13:34
Show Gist options
  • Save chivandikwa/da1e3d5af5e73017b047f5a94b94f76c to your computer and use it in GitHub Desktop.
Save chivandikwa/da1e3d5af5e73017b047f5a94b94f76c to your computer and use it in GitHub Desktop.
Console Bot (Microsoft Bot Framework withou aspnet core)
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
namespace ConsoleBot
{
public class ConsoleActivityHandler :
ActivityHandler
// An implementation of the IBot interface intended for further subclassing
// Derive from this class to plug in code to handle particular Activity types.
// Pre and post processing of Activities can be plugged in by deriving and calling
// the base class implementation.
{
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext,
CancellationToken cancellationToken)
{
await base.OnMessageActivityAsync(turnContext, cancellationToken);
await turnContext.SendActivityAsync($"You sent '{turnContext.Activity.Text}'", cancellationToken: cancellationToken);
}
}
}
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
namespace ConsoleBot
{
public class ConsoleBot : IBot
{
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
await turnContext.SendActivityAsync($"You sent '{turnContext.Activity.Text}'", cancellationToken: cancellationToken);
}
else
{
await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected", cancellationToken: cancellationToken);
}
}
}
}
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
namespace ConsoleBot
{
public class ConsoleBotAdapter : BotAdapter
{
public async Task ProcessActivityAsync(BotCallbackHandler callback = null)
{
while (true)
{
string msg = Console.ReadLine();
if (msg == null)
{
break;
}
var activity = new Activity
{
Text = msg,
ChannelId = "console",
From = new ChannelAccount(id: "user", name: "Jane Doe"),
Recipient = new ChannelAccount(id: "bot", name: "Bot"),
Conversation = new ConversationAccount(id: "Customer Survey Conversation"),
Timestamp = DateTime.UtcNow,
Id = Guid.NewGuid().ToString(),
Type = ActivityTypes.Message
};
using (var context = new TurnContext(this, activity))
{
// Starts activity processing for the current bot turn
await RunPipelineAsync(context, callback, default(CancellationToken)).ConfigureAwait(false);
}
}
}
public override async Task<ResourceResponse[]> SendActivitiesAsync(ITurnContext context, Activity[] activities, CancellationToken cancellationToken)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (activities == null)
{
throw new ArgumentNullException(nameof(activities));
}
if (activities.Length == 0)
{
throw new ArgumentException("Expecting one or more activities, but the array was empty.", nameof(activities));
}
var responses = new ResourceResponse[activities.Length];
for (var index = 0; index < activities.Length; index++)
{
Activity activity = activities[index];
switch (activity.Type)
{
case ActivityTypes.Message:
{
IMessageActivity message = activity.AsMessageActivity();
if (message.Attachments != null && message.Attachments.Any())
{
var attachment = message.Attachments.Count == 1 ? "1 attachment" : $"{message.Attachments.Count()} attachments";
Console.WriteLine($"{message.Text} with {attachment} ");
}
else
{
Console.WriteLine($"{message.Text}");
}
}
break;
case ActivityTypesEx.Delay:
{
int delayMs = (int)activity.Value;
await Task.Delay(delayMs, cancellationToken).ConfigureAwait(false);
}
break;
case ActivityTypes.Trace:
break;
default:
Console.WriteLine("Bot: activity type: {0}", activity.Type);
break;
}
responses[index] = new ResourceResponse(activity.Id);
}
return responses;
}
public override Task<ResourceResponse> UpdateActivityAsync(ITurnContext turnContext, Activity activity, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
public override Task DeleteActivityAsync(ITurnContext turnContext, ConversationReference reference, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}
using System;
namespace ConsoleBot
{
internal class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Welcome to the EchoBot. Type something to get started.");
var adapter = new ConsoleBotAdapter();
//var consoleBot = new ConsoleBot();
var consoleBot = new ConsoleActivityHandler();
adapter.ProcessActivityAsync(
async (turnContext, cancellationToken) => await consoleBot.OnTurnAsync(turnContext,cancellationToken)).Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment