Kahla.SDK

Kahla.SDK is a library for writting bots and extends for Kahla.

NuGet version (Kahla.SDK) Build status

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

demo

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!

demo

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;