Search code examples
c#winformsbotframeworkazure-bot-servicedirect-line-botframework

Facing Issues in WinForm Integration with my Existing Azure Bot Using Directline API


I have created a bot using Bot Framework SDK (C#, .Net). I have published it in Azure. It is working fine in WebChat as well as in Teams channel. Now I want to create one Winform and trying to connect the Winform UI to my bot using Directline Api.

This is my code :

public partial class Form1 : Form {

    private static string directLineSecret = ConfigurationSettings.AppSettings["DirectLineSecret"];

    private static string botId = ConfigurationSettings.AppSettings["BotId"];

    private static string fromUser = "User";
    private static string id = "default-user";

    private Conversation conversation;
    DirectLineClient directLineClient = null;
    

    public Form1()
    {
        InitializeComponent();
        InitClientAsync();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private async Task InitClientAsync()
    {
        directLineClient = new DirectLineClient($"{directLineSecret}");
        directLineClient.BaseUri = new Uri($"https://directline.botframework.com/");

        //directLineClient = new DirectLineClient(directLineSecret);
        conversation = await directLineClient.Conversations.StartConversationAsync().ConfigureAwait(false);

            new System.Threading.Thread(async () => await ReadBotMessageAsync(directLineClient, conversation.ConversationId)).Start();
        
    }

    private async Task ReadBotMessageAsync(DirectLineClient client, string conversationId)
    {
        string watermark = null;

        while(true)
        {
            var activitySet = await client.Conversations.GetActivitiesAsync(conversationId, watermark);
            watermark = activitySet?.Watermark;

            var activities = from x in activitySet.Activities where x.From.Id == botId select x;

            foreach(Activity activity in activities)
            {
                if(activity.Text != null)
                {
                    string message = activity.Text;
                    if(InvokeRequired)
                    {
                        BeginInvoke(new MethodInvoker(delegate
                        {
                            textBox.AppendText("Bot : " + message + "\r\n");
                        }));
                    }
                    
                }
            }
        }
    }

    private async void btn_Send_Click(object sender, EventArgs e)
    {
        string input = txt_Send.Text;
        txt_Send.Clear();
        if(input.Length>0)
        {
            Activity userMessage = new Activity
            {
                From = new ChannelAccount(id, fromUser),
                Text = input,
                Type = ActivityTypes.Message,
                TextFormat = "plain"
            };
            textBox.AppendText("You : " + input + "\r\n");
            await directLineClient.Conversations.PostActivityAsync(this.conversation.ConversationId, userMessage);
        }
    }
}

But when I execute the program it is stopping due to Null reference exception when it is trying to get conversation as well as conversation Id.

you can check the error image here.

Can anyone please tell me what I'm missing here? Any help will be appreciated.....


Solution

  • The problem was with the directline secret key. I regenerate a new key and used it. Now that is starting the conversation and I am able to see conversation Id.

    But after that I was getting status code 429 error. That is basically Multiple thread issue. It is trying to create multiple threads at the same time. Then I recreate the whole project with .Net Framework version 4.6.2 (Previously using 4.7). Now that issue also got resolved. Project is working fine.