gabrieldwight / Whatsapp-Business-Cloud-Api-Net

This is C# wrapper of whatsapp business cloud api for .NET
MIT License
306 stars 120 forks source link

Flow support #52

Closed borrmann closed 1 year ago

borrmann commented 1 year ago

Support of flow messages, see here: https://developers.facebook.com/docs/whatsapp/flows/gettingstarted/sendingaflow

Describe alternatives you've considered Not sure how much is necessarry, as it looks like they can be send similarly to interactive messages. If it is already possible to send them, an example in the README would be very much appreciated.

gabrieldwight commented 1 year ago

I will have a look at the documentation for the flow messages creation and sending to check how it works

borrmann commented 1 year ago

here is an example how I currently send my (DataExchange) flows without this library:

var request = new HttpRequestMessage(HttpMethod.Post, $"https://graph.facebook.com/v18.0/{_waPhoneNr}/messages");
request.Headers.Add("Authorization", $"Bearer {_waToken}");

var flowButton = new BotMessageFlowButton()
            {
                ButtonText = "Update billing info",
                IsInDraftMode = false,
                FlowVersion = "3",
                FlowActionType = FlowActionTypes.DataExchange,
                FlowId = "<flowId>",
                FlowScreen = "Billing_Address",
                Data = new
                {
                    MyDataId1 = "somestringId",
                    MyDataId2 = 20
                }
            }

var dynamicContent = new
{
    recipient_type = "individual",
    messaging_product = "whatsapp",
    to = identifier,
    type = "interactive",
    interactive = new
    {
        type = "flow",
        header = new
        {
            type = "text",
            text = header
        },
        body = new
        {
            text = message
        },
        action = new
        {
            name = "flow",
            parameters = new
            {
                mode = flowButton.IsInDraftMode ? "draft" : "published",
                flow_message_version = "3",
                flow_token = flowButton.FlowResponseId,
                flow_id = flowButton.FlowId,
                flow_cta = flowButton.ButtonText,
                flow_action = "navigate",
                flow_action_payload = new
                {
                    screen = flowButton.FlowScreen,
                    data = flowButton.Data
                }
            }
        }
    }
};

var stringCont = JsonConvert.SerializeObject(dynamicContent);
var content = new StringContent(stringCont, Encoding.UTF8, "application/json");                        

request.Content = content;
var responsef = await _httpClient.SendAsync(request);

where flowButton is of type:

public class BotMessageFlowButton
{
    public required string ButtonText { get; init; }
    public required bool IsInDraftMode { get; init; }
    public required string FlowId { get; init; }
    public required string FlowVersion { get; init; }
    public required string FlowScreen { get; init; }

    public required object Data { get; init; }
    public required FlowActionTypes FlowActionType { get; init; }

    public enum FlowActionTypes
    {
        DataExchange,
        Navigation
    };
}
gabrieldwight commented 1 year ago

Thanks for the example. Almost done implementing the flow message support. Remaining to add the support for creating the flow message types so I will use your example to finish up this part.