Skip to content

ArgedInteractions Code Snippets

SKitLs-dev edited this page Mar 30, 2024 · 3 revisions

Contents

Add Serialize Service

To use project's facilities add new IArgsSerializeService to Bot's Services:

BotBuilder.NewBuilder("your_token")
    // ...
    .AddService<IArgsSerilalizerService>(new ArgsSerilalizerService())
    .Build();

Last review: .ArgedInteractions v1.5.3 Back to contents

Convert Rules

Function-Oriented (ConvertRule TOut)

var fooRule = new ConvertRule<Foo>(
    (input) =>
    {
        if (string.IsNullOrEmpty(input))
            return ConvertResult<MyCustomDataType>.NullInput();

        MyCustomDataType customDataInstance;
        // Custom logic to convert the string input into MyCustomDataType
        // ... (implementation details)
        if (customDataInstance is null)
            return ConvertResult<MyCustomDataType>.Incorrect();
        else
            return ConvertResult<MyCustomDataType>.OK(customDataInstance);
    }),

Class-Oriented (ConverterBase TOut)

public class MyCustomDataTypeConverter : ConverterBase<MyCustomDataType>
{
    public override ConvertResult<MyCustomDataType> Converter(string input)
    {
        if (string.IsNullOrEmpty(input))
            return ConvertResult<MyCustomDataType>.NullInput();

        MyCustomDataType customDataInstance;
        // Custom logic to convert the string input into MyCustomDataType
        // ... (implementation details)
        if (customDataInstance is null)
            return ConvertResult<MyCustomDataType>.Incorrect();
        else
            return ConvertResult<MyCustomDataType>.OK(customDataInstance);
    }
}

Last review: .ArgedInteractions v1.5.3 Back to contents

Using Arged Actions

To explore arged actions visit link.

To explore custom implementation check how bot's menus are realized. (TODO: add ref)

public class FooArgs
{
    [BotActionArgument(0)]
    public CustomUser User { get; set; } // User { UserId = 'userid' }

    [BotActionArgument(1)]
    public bool Value { get; set; } // true
}
private BotArgedCallback<FooArgs> ArgedCallback = new("ArgedCall", "Label", Do_ArgedAsync)

private async Task Do_ArgedAsync(FooArgs args, SignedCallbackUpdate update)
{
    // Access args.User or args.Value
}

Last review: .ArgedInteractions v1.5.3 Back to contents

Implementation Examples

Using Args Serializer

BotArgedCallback

public class BotArgedCallback<TArg> : DefaultCallback, IArgedAction<TArg, SignedCallbackUpdate> where TArg : notnull, new()
{
    public BotArgedInteraction<TArg, SignedCallbackUpdate> ArgAction { get; protected set; }

    public BotArgedCallback(string @base, string label, BotArgedInteraction<TArg, SignedCallbackUpdate> action, char splitToken = ';') : base(@base, label, null!)
    {
        Action = MiddleAction;
        ArgAction = action;
        SplitToken = splitToken;
    }

    // ...

    public ConvertResult<TArg> DeserializeArgs(SignedCallbackUpdate update, IArgsSerializeService serializer)
        => serializer.Deserialize<TArg>(update.Data[(update.Data.IndexOf(SplitToken) + 1)..], SplitToken);

    private async Task MiddleAction(SignedCallbackUpdate update)
    {
        var argService = update.Owner.ResolveService<IArgsSerializeService>();
        var args = DeserializeArgs(update, argService);
        if (args.ResultType == ConvertResultType.Ok)
        {
            await ArgAction.Invoke(args.Value, update);
        }
        else
        {
            throw new ArgedInterException("ArgedActionNullValue", SKTEOriginType.External, this, args.ResultMessage);
        }
    }
}

Last review: .ArgedInteractions v1.5.3 Back to contents