gpt4 book ai didi

c# - 从触发器开始对话

转载 作者:行者123 更新时间:2023-11-30 23:15:18 25 4
gpt4 key购买 nike

这是在 C# 中,在 Skype channel 上:

是否可以通过触发器启动与用户的对话?

情况:在定时时刻,我想向所有注册用户显示一个对话框(从中我有一个 resumptioncookie)所以我有一个 azure 函数来监视队列并触发我的机器人。在那个触发器上,我可以发送一条简单的消息,但我想在那一刻开始对话。

[BotAuthentication]
public class MessagesController: ApiController {
public async Task < HttpResponseMessage > Post([FromBody] Activity activity) {
if (activity != null) {
switch (activity.GetActivityType()) {
case ActivityTypes.Message:
//some stuff here, not important for now
break;

case ActivityTypes.Trigger:
//This does not work:
await Conversation.SendAsync(activity, () => new ExceptionHandlerDialog < object > (new BroodjesDialog(), true));

//But this does:
IEventActivity trigger = activity;
var message = JsonConvert.DeserializeObject < Message > (((JObject) trigger.Value).GetValue("Message").ToString());

await Resume((Activity) message.ResumptionCookie.GetMessage());

var messageactivity = (Activity) message.ResumptionCookie.GetMessage();
client = new ConnectorClient(new Uri(messageactivity.ServiceUrl));
var triggerReply = messageactivity.CreateReply();
triggerReply.Text = $ "Let's do some talking";
await client.Conversations.ReplyToActivityAsync(triggerReply);
break;
}
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

最佳答案

查看机器人框架提供的这个 AlarmBot 示例,它展示了如何从外部事件启动对话。

namespace Microsoft.Bot.Sample.AlarmBot.Models
{
/// <summary>
/// This method represents the logic necessary to respond to an external event.
/// </summary>
public static class ExternalEvent
{
public static async Task HandleAlarm(Alarm alarm, DateTime now, CancellationToken token)
{
// since this is an externally-triggered event, this is the composition root
// find the dependency injection container
var container = Global.FindContainer();

await HandleAlarm(container, alarm, now, token);
}

public static async Task HandleAlarm(ILifetimeScope container, Alarm alarm, DateTime now, CancellationToken token)
{
// the ResumptionCookie has the "key" necessary to resume the conversation
var message = alarm.Cookie.GetMessage();
// we instantiate our dependencies based on an IMessageActivity implementation
using (var scope = DialogModule.BeginLifetimeScope(container, message))
{
// find the bot data interface and load up the conversation dialog state
var botData = scope.Resolve<IBotData>();
await botData.LoadAsync(token);

// resolve the dialog stack
var stack = scope.Resolve<IDialogStack>();
// make a dialog to push on the top of the stack
var child = scope.Resolve<AlarmRingDialog>(TypedParameter.From(alarm.Title));
// wrap it with an additional dialog that will restart the wait for
// messages from the user once the child dialog has finished
var interruption = child.Void(stack);

try
{
// put the interrupting dialog on the stack
stack.Call(interruption, null);
// start running the interrupting dialog
await stack.PollAsync(token);
}
finally
{
// save out the conversation dialog state
await botData.FlushAsync(token);
}
}
}
}
}

关于c# - 从触发器开始对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42674217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com