botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Is there any code (example) for creating persistent menu with microsoft bot framework c# ?

Open ghost opened this issue 9 years ago • 16 comments

Is there any code for creating persistent menu with microsoft bot framework c# ? I found this : https://satwikkansal.wordpress.com/2016/08/25/adding-menu-and-buttons-to-your-messenger-bot/ But i'm using c# language .... so it would be excellent if is there any example written in c#

ghost avatar Feb 20 '17 13:02 ghost

Not out of the box - curl is just making a HTTP Post to the FB API ...

Why don't you just Postman or Fiddler (or write some c# code) and create the necessary POST request - we used that method with great success.

Agree that it would be nice to add this to the emulator or even the bot framework portal. PS. Remember that you would have to 'catch' these 'global' commands as they may appear in any dialog/conversation you are having.

leroibeukes avatar Feb 23 '17 15:02 leroibeukes

@lonche Hi, I'm having the same difficulty. Were you able to make the menu in C #? Best regards

krconnect avatar Jun 05 '17 16:06 krconnect

@krconnect Yes. I made persistent menu. It is more difficult if you want to make POST request to Facebook using c#. If you want to make persistent menu, use this : https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu and (ex. fiddler).

ghost avatar Jun 06 '17 06:06 ghost

@lonche This documentation I already read, but I have not yet been able to understand how to create a drop down menu in C #. Thanks for the answer, I'll search more.

krconnect avatar Jun 06 '17 11:06 krconnect

Hi Guys

Don't see what the issue is ? - Just do it via fiddler/Postman or whatever.

If you really want c# code -> use webclient and upload the data as specified in the links (substitute the curl for the webclient code).

To help you out I made you a quick sample using the FB Get started payload (which you need for the persistent menu) http://rextester.com/PGZ32787

Code:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Http;
using System.Dynamic;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            String PAGE_ACCESS_TOKEN = "MYTOKEN"; //your facebook app token
         
            //Build JSON required - Or use a string , copy+paste
            dynamic Data = new ExpandoObject();
            Data.get_started = new ExpandoObject();
            Data.get_started.payload = "GET_STARTED_PAYLOAD";
            
            Console.WriteLine("About to send : " + Newtonsoft.Json.JsonConvert.SerializeObject(Data));
            
            using (WebClient client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var result = client.UploadString(
                    "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=" + PAGE_ACCESS_TOKEN,
                    Newtonsoft.Json.JsonConvert.SerializeObject(Data)
                );
                Console.WriteLine("Result : " + result);
            } 
        }
    }
}```

If you can't see how to do the rest using this method then I'm not sure how we can assist further.

Cheers

leroibeukes avatar Jun 06 '17 15:06 leroibeukes

@leroibeukes Thank you my friend!!!!! Now you've cleared the way! Many, many thanks!

krconnect avatar Jun 06 '17 15:06 krconnect

@leroibeukes Many thanks, you could also share for Persistent Menu. please. in C#

Yudner avatar Jun 27 '17 00:06 Yudner

Can anyone please help share code in c# for persistent menu? Thanks in advance.

amitger avatar Jun 30 '17 05:06 amitger

I understood the process, but when I went to perform I could not. I'm still trying to build this menu that looks so simple, but it's not!

krconnect avatar Jun 30 '17 11:06 krconnect

Hi Guys ....

Ok... I'll help you out and provide you with some sample code --- Expect it in the next day or two.

Cheers Le Roi

leroibeukes avatar Jun 30 '17 11:06 leroibeukes

@leroibeukes .. please share the code.

amitger avatar Jul 13 '17 12:07 amitger

Sorry guys - been travelling , will get to it in the next day or two

leroibeukes avatar Jul 13 '17 13:07 leroibeukes

tks

krconnect avatar Jul 13 '17 13:07 krconnect

Similar to last one - just build the structure...

Note did not test this very well.

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Http;
using System.Dynamic;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            String PAGE_ACCESS_TOKEN = "MYTOKEN"; //your facebook app token
         
            //Build JSON required - Or use a string , copy+paste
           
            dynamic Data = new ExpandoObject();
            Data.persistent_menu = new ExpandoObject();
            Data.persistent_menu = new []
            {
                new { locale = "default",
				      composer_input_disabled = false,
                      call_to_actions = new []
                      {
                          new { type  = "postback",
                                title = "Menu Free Text",
                                payload = "menu1",//This is what the bot will receive
                              },
                          new { type  = "postback",
                            title = "Menu Free Text2",
                            payload = "menu2",
                              
                          },
                              /* DIFFERENT MENU STRUCTURE */
                              /*
                          new { 
                              type  = "nested",
                              title = "More",
                              
                              call_to_actions = new []
                              {
                                  new { type  = "web_url",
                                        title = "CLICK ME",
                                        url = "https://SomeURLThatWasWhiteListedINYOURFBDEVELOPERPROFILE.com",
                                        webview_height_ratio= "full",
								        messenger_extensions = true
                                      }                                  
                              }
                          }*/
                      }
                    }   
            };
            
            
            Console.WriteLine("About to send : " + Newtonsoft.Json.JsonConvert.SerializeObject(Data));
            
            using (WebClient client = new WebClient())
            {
                client.Headers[HttpRequestHeader.ContentType] = "application/json";
                var result = client.UploadString(
                    "https://graph.facebook.com/v2.6/me/messenger_profile?access_token=" + PAGE_ACCESS_TOKEN,
                    Newtonsoft.Json.JsonConvert.SerializeObject(Data)
                );
                Console.WriteLine("Result : " + result);
            } 
        }
    }
}

leroibeukes avatar Jul 13 '17 21:07 leroibeukes

Perfect! Menu created !!! Now I'm trying to get the return from the menu click!

krconnect avatar Jul 17 '17 14:07 krconnect

Awesome- please let us know how you go .

You should receive as the payload "menu1" when you click the "Menu Free Text" button.

Use this to figure out which action to do.

Cheers

leroibeukes avatar Jul 18 '17 10:07 leroibeukes