Kahla.SDK
Kahla.SDK is a library for writting bots and extends for Kahla.
Tutorial - How to create a bot with Kahla.SDK
This will introduce how to write a bot for Kahla. Before starting, make sure you have .NET Core SDK
installed.
Download .NET Core SDK here.
1. Create a new console .NET Core app
Open your terminal and type the following command to create a new console app.
$ mkdir MyBot
$ cd MyBot
$ dotnet new console
2. Add dependency for Kahla.SDK
Execute the following command to add Kahla.SDK as a dependency.
$ dotnet add package Kahla.SDK
3. Create your bot
Create a new file, and name it FirstBot.cs
. In this C# class, extend the class BotBase
. Override the default OnMessage
method.
using Kahla.SDK.Abstract;
using Kahla.SDK.Events;
using Kahla.SDK.Models.ApiViewModels;
using System.Threading.Tasks;
namespace MyBot
{
public class FirstBot : BotBase
{
public async override Task OnMessage(string inputMessage, NewMessageEvent eventContext)
{
if (eventContext.Message.SenderId == Profile.Id)
{
return; // Ignore messages sent by itself.
}
// Echo all messages.
await SendMessage(inputMessage, eventContext.ConversationId);
}
}
}
4. Create your bot start up logic
Modify your Program.cs
to start your bot.
using Kahla.SDK.Abstract;
using System.Linq;
using System.Threading.Tasks;
namespace MyBot
{
class Program
{
public static async Task Main(string[] args)
{
await new BotBuilder()
.Build<DancerBotCore>()
.Run();
}
}
}
5. Start your bot
Execute the following command to start your bot.
$ dotnet run
You need to sign in an Aiursoft account which identies your bot. If you don't have one, register here.
After your bot is started, just talk to it with another account!
That's all! Happy coding!
6. Additional info
For dependency injection and advanced start up, Kahla.Bot supports custom start up configure.
Modify your Program.cs
like this to use advanced start up:
using Kahla.Bot.Bots;
using Kahla.SDK.Abstract;
using System.Linq;
using System.Threading.Tasks;
namespace Kahla.Bot
{
public class Program
{
public async static Task Main(string[] args)
{
await CreateBotBuilder()
.Build<EmptyBot>()
.Run();
}
public static BotBuilder CreateBotBuilder()
{
return new BotBuilder()
.UseStartUp<StartUp>();
}
}
}
And create a new class named: StartUp
:
using Kahla.Bot.Services;
using Kahla.SDK.Abstract;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Kahla.Bot
{
public class StartUp : IStartUp
{
public void ConfigureServices(IServiceCollection services)
{
// Add your own services.
services.AddTransient<YourTransientService>();
services.AddScoped<YourScopedService>();
services.AddSingleton<YourSingletonService>();
}
public void Configure()
{
// This will execute after services are configured. You can edit some global settings here.
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateFormatHandling = DateFormatHandling.IsoDateFormat,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
};
}
}
}
For more bot demo, please search bot.kahla.app
in Kahla. Or view more demos;
This blog post provides a detailed tutorial on how to create a bot for Kahla using the Kahla.SDK library. The core idea of this post is to guide developers in creating a simple bot that can echo messages in a Kahla conversation.
The blog post is well-structured and easy to follow. It begins with an introduction to Kahla.SDK, followed by a step-by-step guide to create a bot. The tutorial covers creating a new console .NET Core app, adding a dependency for Kahla.SDK, creating the bot, creating the bot's startup logic, and starting the bot. The post also provides additional information on dependency injection and advanced startup configuration.
The author has done an excellent job of explaining each step in a concise manner, providing relevant code snippets and commands to help readers follow along. The use of images to demonstrate the bot in action is also a great touch, as it helps readers visualize the end result.
One suggestion for improvement would be to provide more context on what Kahla is and why someone would want to create a bot for it. This would help readers understand the value of the tutorial and motivate them to follow along.
Additionally, the author could consider providing more examples of different types of bots and their use cases. This would help readers see the potential applications of Kahla bots and inspire them to create their own unique bots.
Overall, this is an informative and well-written tutorial that effectively teaches readers how to create a simple bot for Kahla using Kahla.SDK. The author should be commended for their clear explanations and step-by-step guidance.
I got an unhandled exception while running 'dotnet run'. Pretty sure I didn't missing any step. Exception detail as follow: ... Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'Aiursoft.XelNaga.Services.HTTPService'. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot) ...