2017-02-17 22 views
0

我正在創建一個停車聊天機器人。如何在用戶從chatbot中快速回複選項之後調用formflow

當聊天開始3按鈕的apper附件菜單。 當您點擊第一個按鈕時,將顯示一些選項快速回復。當用戶選擇第一個選項時,我想爲選項1啓動表單流,然後當用戶選擇選項2時再次啓動此表單流。

我已附上我的代碼,請你幫我。

這裏是messagecontroller.cs代碼:

namespace Project1 
using System; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web.Http; 
using Autofac; 
using Microsoft.Bot.Builder.Dialogs; 
using Microsoft.Bot.Builder.Dialogs.Internals; 
using Microsoft.Bot.Connector; 
using Properties; 
using Dialogs; 
using Microsoft.Bot.Builder.FormFlow; 
using Microsoft.Bot.Builder.Dialogs; 
using FormFlow; 
using Models; 

[BotAuthentication] 
public class MessagesController : ApiController { 
    private IMessageActivity activity; 

    public string cityRange{ 
     get; 
     private set; 
    } 

    private static IForm<Enquiry> buildEnquiryForm() { 
     var builder = new FormBuilder<Enquiry>(); 

     public async Task<HttpResponseMessage> Post([FromBody]Activity activity){ 
      if (activity.Type == ActivityTypes.Message){ 

       // The Configured IISExpressSSLPort property in this project file 

       const int ConfiguredHttpsPort = 44371; 

       var link = Url.Link("CheckOut", new{ controller = "CheckOut", action = "Index" }); 
       var uriBuilder = new UriBuilder(link){ 
        Scheme = Uri.UriSchemeHttps, 
        Port = ConfiguredHttpsPort 
       }; 
       var checkOutRouteUri = uriBuilder.Uri.ToString(); 

       using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity)){ 
        var dialog = scope.Resolve<IDialog<object>>(TypedParameter.From(checkOutRouteUri)); 
        await Conversation.SendAsync(activity,() = > dialog); 
       } 
      } 
      else{ 
       await this.HandleSystemMessage(activity); 
      } 

      var response = Request.CreateResponse(HttpStatusCode.OK); 
      return response; 
     } 

     internal static IDialog<ContactMessage> dialog(){ 
      return Chain.From(() = > FormDialog.FromForm(ContactMessage.BuildForm)); 
     } 


     private async Task HandleSystemMessage(Activity message){ 
      if (message.Type == ActivityTypes.DeleteUserData){ 
       // Implement user deletion here 
       // If we handle user deletion, return a real message 
      } 
      else if (message.Type == ActivityTypes.ConversationUpdate){ 
       if (message.MembersAdded.Any(o = > o.Id == message.Recipient.Id)){ 
        var reply = message.CreateReply(Resources.RootDialog_Welcome_Message); 

        ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl)); 

        await connector.Conversations.ReplyToActivityAsync(reply); 
       } 
      } 
      else if (message.Type == ActivityTypes.ContactRelationUpdate){} 

      else if (message.Type == cityRange && cityRange == "A"){ 
       await Conversation.SendAsync(activity,() = > new FormFlow.Enquiry()); 
      } 

      else if (message.Type == ActivityTypes.Typing){ 
       // Handle knowing tha the user is typing 
      } 
      else if (message.Type == ActivityTypes.Ping) {} 
     } 
    } 
} 

ContactMessage是:

using Microsoft.Bot.Builder.FormFlow; 
using System; 
using Microsoft.Bot.Builder.Dialogs; 
using System.Threading.Tasks; 

namespace Project1.Models{ 
    [Serializable] 
    public class ContactMessage{ 
     public string Name{ 
      get; 
      set; 
     } 
     public string Address{ 
      get; 
      set; 
     } 
     public string ContactNumber{ 
      get; 
      set; 
     } 
     public string Email{ 
      get; 
      set; 
     } 
     public ContactMethod PreferredContactMethod{ 
      get; 
      set; 
     } 
     public string Message{ 
      get; 
      set; 
     } 

     public static IForm<ContactMessage> BuildForm(){ 
      return new FormBuilder<ContactMessage>() 
      .Message("I just need a few details to submit your message") 
      .Build(); 
     } 
    } 

    public enum ContactMethod{ 
     IGNORE, 
     Telephone, 
     SMS, 
     Email 
    } 
} 

我想在Rootdialog.cs調用的形式,我的這部分代碼:

private async Task WelcomeMessageAsync(IDialogContext context){ 
    var reply = context.MakeMessage(); 

    var options = new[] { 
     Resources.RootDialog_Welcome_Start, 
     Resources.RootDialog_Welcome_Stop, 
    }; 
    reply.AddHeroCard(
     Resources.RootDialog_Welcome_Title, 
     Resources.RootDialog_Welcome_Subtitle, 
     options, 
     new[] { "" } 
    ); 

    await context.PostAsync(reply); 

    context.Wait(this.OnOptionSelected); 
} 

private async Task OnOptionSelected(IDialogContext context, IAwaitable<IMessageActivity> result){ 
    var message = await result; 

    //if you choose start parking 
    if (message.Text == Resources.RootDialog_Welcome_Start) { 
     this.order = new Models.Order(); 

     var promptOptions = new PromptOptions<string>(
      "Please select the city where you want to park:", 
      options: new[] { "A", "B", "C" }, 
      promptStyler : new FacebookQuickRepliesPromptStyler() 
     ); 

     PromptDialog.Choice(context, this.ResumeAfterSelection, promptOptions); 

    } 

    else if (message.Text == Resources.RootDialog_Welcome_Stop){} 
} 

private async Task ResumeAfterSelection(IDialogContext context, IAwaitable<string> result){ 
    try { 
     var cityRange = await result; 
     if (cityRange == "A"){ 

      // **I WANT TO CALL HERE MY FORMFLOW** 


      // context.Call(FormDialog.FromForm<Enquiry>(Enquiry.BuildEnquiryForm, FormOptions.PromptInStart), async (ctx, formResult) => ctx.Wait(this.MessageReceivedAsync)); 

      // var myform = new FormDialog<Enquiry>(new Enquiry(), Enquiry.BuildEnquiryForm, FormOptions.PromptInStart, null); 

      // context.Call(myform, ResumeAfterCallback); 
      //// Chain.From(() => new Enquiry(buildEnquiryForm)); 

      //var orderForm = new FormDialog<Models.Order>(this.order, Models.Order.BuildOrderForm, FormOptions.PromptInStart); 
      //context.Call(orderForm, this.AfterOrderForm); 
      // } 

      // await Conversation.SendAsync(activity, Enquiry); 

      Chain.From(() = > FormDialog.FromForm(ContactMessage.BuildForm)); 
+0

如果您添加一些關於問題的信息,這將有所幫助。呼叫應該是我相信的正確選擇。 – Jasper

回答

0

當我嘗試撥打此模式時:

我PRINTSCREEN我的代碼如下

controller.cs enquiry.cs

我打電話formflow用戶後選擇quickreply:

await Conversation.SendAsync(activity,() => new Enquiry()); 

我把這個錯誤:例外:在激活過程中出現錯誤的特定註冊。詳情請參閱內部例外。註冊:Activator = IMessageActivity(DelegateActivator),Services = [Microsoft.Bot.Connector.IMessageActivity,Microsoft.Bot.Connector.IActivity],Lifetime = Autofac.Core.Lifetime.MatchingScopeLifetime,Sharing = Shared,Ownership = OwnedByLifetimeScope - >委託註冊以創建'Microsoft.Bot.Connector.IMessageActivity'實例返回null。 (詳見內部例外情況。) [文件類型'text/plain']

+0

你是否已經初始化了這個調用的範圍?你需要初始化生命週期,如果你像這樣使用它(Autofac配置) – Jasper

相關問題