使用Directline API与我现有的Azure Bot面对Winform集成的问题
我使用BOT Framework SDK(C#,.NET)创建了一个机器人。我已经用Azure出版了它。它在网络聊天和团队频道中都可以正常工作。现在,我想创建一个Winform,并尝试使用DirectLine API将Winform UI连接到我的机器人。
这是我的代码:
公共部分类Form1:表格 {
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);
}
}
}
但是当我执行程序时,由于null引用异常而在尝试进行对话以及对话ID时停止。
您可以检查错误image 在此处。
谁能告诉我我在这里想念什么? 任何帮助将不胜感激.....
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(quot;{directLineSecret}");
directLineClient.BaseUri = new Uri(quot;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.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于直线秘密密钥。我再生一个新钥匙并使用了它。现在,这开始对话,我可以看到对话ID。
但是之后,我获得了状态代码429错误。这基本上是多个线程问题。它正在尝试同时创建多个线程。
然后,我使用.NET Framework版本4.6.2(以前使用4.7)重新创建整个项目。现在,这个问题也得到了解决。项目工作正常。
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.