简体   繁体   中英

Microsoft Bot State data store to SQL Server database using C#

How to store the bot state data to a SQL Server database? I have this code which performs form flow action and collects user inputs. These data I need to save it to database and show the user with a Request ID for future reference. As I'm new to these things, got struck on this stage.

Below is the code I'm working on.

// Hardware
public enum HardwareOptions
{
    Mouse, Printer, Keyboard, Monitor
};

[Serializable]
public class HardwareQuery
{
    [Prompt("Choose your {&} ? {||}")]
    public HardwareOptions? Hardware;

    [Prompt("Please enter {&}")]
    public string Details { get; set; }

    [Prompt("Please enter {&}")]
    public int Quantity { get; set; }

    [Prompt("Please provide your business need / {&} below")]
    public string Justification { get; set; }

    public static IForm<HardwareQuery> BuildForm()
    {
        return new FormBuilder<HardwareQuery>()
                .Message("Welcome to the PHI Create Hardware Request!")
                .Build();
    }
}

How to store the bot state data to a SQL Server database?

The Azure Extensions allow us to store bot's state data in Azure SQL , you can install and use it with you bot application. Besides, if you'd like to implement/customize BotDataStore with your own SQL server database, you can check the sourc code of Azure Extensions on GitHub to know how to implement the IBotDataStore interface in order to use a SQL database.

These data I need to save it to database and show the user with a Request ID for future reference.

If you just want to save the user inptes that you collect from HardwareQuery Form, you can get user inputs and save into database on completion method.

public static IForm<HardwareQuery> BuildForm()
{
    return new FormBuilder<HardwareQuery>()
            .Message("Welcome to the PHI Create Hardware Request!")
                .OnCompletion(async (context, HardwareQueryForm) =>
                {
                    //you code logic 

                    string hardware = HardwareQueryForm.Hardware.ToString();
                    string details = HardwareQueryForm.Details;
                    int quantity = HardwareQueryForm.Quantity;
                    string justification = HardwareQueryForm.Justification;

                    //connect to database and save user inputs into database
                })
                .Build();
}

For Bot Framework SDKv4:

As written here you need to implement the IStorage interface.

You can take the example I made here as a blueprint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM