namespace KakaoChatBotStudy;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
// Data class
namespace KakaoChatBotStudy.Data;
public class TextCardData
{
public string? Title { get; set; }
public string? Description { get; set; }
public string? ButtonLabel { get; set; }
public string? ButtonWebLink { get; set; }
}
// Server
using KakaoChatBotStudy.Extension;
using KakaoChatBotStudy.KakaoAPI;
using Microsoft.AspNetCore.Mvc;
namespace KakaoChatBotStudy.Controllers;
[ApiController]
[Route("[controller]")]
public class TextCardTest : ControllerBase
{
[HttpPost]
public SkillResponse Post([FromBody] SkillPayload payload)
{
Console.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd HH.mm.ss")}] Text card requested - Request user id: {payload.UserRequest!.User!.Id}");
SkillResponse response = new();
try
{
var data = payload.Action!.Params!.ToTextCardData();
var skillTemplate = new SkillTemplate
{
Outputs = new()
};
TextCard card = new()
{
Title = data.Title,
Description = data.Description,
Buttons =
[
new ButtonWithWebLink()
{
Label = data.ButtonLabel,
WebLinkUrl = data.ButtonWebLink
}
]
};
skillTemplate.Outputs.AddComponent(card);
response.Template = skillTemplate;
}
catch (Exception e)
{
Console.WriteLine(e);
}
return response;
}
}
// Extension methods
using KakaoChatBotStudy.Data;
using KakaoChatBotStudy.KakaoAPI;
namespace KakaoChatBotStudy.Extension;
public static class UserInputDataExtension
{
public static TextCardData ToTextCardData(this Dictionary<string, string> dic)
{
return new()
{
Title = dic[nameof(TextCardData.Title).ToFirstLetterLower()],
Description = dic[nameof(TextCardData.Description).ToFirstLetterLower()],
ButtonLabel = dic[nameof(TextCardData.ButtonLabel).ToFirstLetterLower()],
ButtonWebLink = dic[nameof(TextCardData.ButtonWebLink).ToFirstLetterLower()]
};
}
}
public static class SkillTemplateExtension
{
public static void AddComponent(this List<Dictionary<string, Component>> outputs, Component component)
{
outputs?.Add(new() { { component.GetType().Name.ToFirstLetterLower(), component } });
}
}