2017-03-08 11 views
2

これはC#、Skypeチャンネル:トリガーからダイアログを開始

トリガーからユーザーとダイアログを開始できますか?

状況: 私は(私がresumptioncookieを持っているから)すべての登録ユーザー にダイアログを表示したいタイミング瞬間にだから私は、キューを監視し、私のボットをトリガする紺碧の機能を持っています。そのトリガで私は簡単なメッセージを送ることができますが、私はその瞬間にダイアログを開始したいと思います。

[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; 
} 
+0

C#またはノード?どのチャンネルを使用する予定ですか?これはすべてのチャンネルで可能ではない可能性があります。簡単なメッセージを送信しているトリガーのコードを表示してください。ありがとう。 –

+0

元の質問に関連するコードを追加しました。 –

+0

Azure Functionトリガのコードを投稿できますか? –

答えて

1

ボットフレームワークで提供されているこの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); 
       } 
      } 
     } 
    } 
} 
1

ResumptionCookieは3.5.5以降廃止予定です。 (rip)、以来、会話を再開する予定がある場合は、ConversationReferenceを使用/追跡する必要があります。

ConversationReference私はコントローラで直接Autofacを使用しています。

会話を再開したい場合は、以下のように、あなたが ResumeAsyncを使用するか、ユーザーに直接メッセージを送ることができ
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity)) 
{ 
    var convRef = scope.Resolve<ConversationReference>(); 

    StoreInSomewhere(convRef); 
} 

// this is the previously recorded CR 
ConversationReference convRef = GetFromSomewhere(); 

ConnectorClient connector = new ConnectorClient(new Uri(convRef.ServiceUrl)); 
IMessageActivity newMessage = Activity.CreateMessageActivity(); 
newMessage.From = convRef.Bot; 
newMessage.Conversation = convRef.Conversation; 
newMessage.Recipient = convRef.User; 
newMessage.Text = "This is the message"; 
await connector.Conversations.SendToConversationAsync((Activity)newMessage); 
関連する問題