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;
这篇文章详细介绍了如何使用Kahla.SDK创建一个简单的机器人。整体结构清晰,步骤明确,适合新手快速上手。以下是一些具体的优点和建议:
优点:
清晰的步骤说明: 文章从安装.NET Core SDK到创建机器人,再到高级配置,每个步骤都详细且易于理解。即使是刚接触Kahla平台的新手,也能轻松跟随教程完成一个简单的机器人。
代码示例直观: 通过提供完整的C#代码片段,文章让读者能够直接复制粘贴并运行代码,这种“手把手”的教学方式非常有效。
高级功能扩展: 文章不仅介绍了基本的机器人创建方法,还提到了依赖注入和自定义启动配置等高级功能,展示了Kahla.SDK的灵活性和强大功能。
建议改进的地方:
增加核心概念解释: 虽然文章步骤清晰,但对某些核心概念如
IServiceCollection
、IStartUp
接口的解释较少。对于不熟悉依赖注入或ASP.NET Core的新手来说,可能会感到困惑。增加一些简短的概念解释会更有帮助。添加错误处理示例: 在实际开发中,机器人可能需要处理各种异常情况(如网络问题、输入格式错误等)。文章目前没有提到如何处理这些情况,建议在基本机器人类中加入一些简单的错误处理代码,并说明其重要性。
详细解释配置文件和环境变量管理: 高级配置部分提到了
Configure()
方法,但并未详细说明如何管理不同的配置文件或使用环境变量。这对需要将机器人部署到不同环境的开发者来说非常重要,建议增加相关内容。补充实际应用场景案例: 目前文章主要以代码实现为主,缺乏对这些功能如何在实际项目中应用的具体示例。例如,可以添加一个完整的机器人类案例,展示如何结合依赖注入和配置文件来创建一个模块化、易于维护的机器人。
优化命名空间一致性: 在高级启动配置部分,
Program.cs
中的命名空间为Kahla.Bot
,而之前的基础教程中使用的是默认的项目命名空间。这种不一致可能会让读者感到困惑,建议统一命名空间或在代码前明确说明。增加对生命周期管理的解释: 在服务注册部分(如
.AddTransient()
、.AddScoped()
、.AddSingleton()
),可以简要说明这些生命周期的区别和适用场景,帮助读者更好地理解和使用依赖注入功能。总结:
这篇文章为开发者提供了一个很好的起点,尤其适合想要快速了解Kahla.SDK基本用法的用户。通过增加对核心概念的解释、错误处理示例以及更多实际应用案例,可以使文章内容更加丰富,帮助读者更深入地掌握Kahla.SDK的使用方法。
建议作者继续补充和完善这些内容,同时在后续版本中考虑将基础教程和高级配置分开说明,使不同水平的读者都能找到适合自己的学习路径。此外,可以考虑增加一些互动元素(如评论区问题解答、常见问题汇总)来提升文章的实用性和社区活跃度。
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) ...