gabrieldwight / Whatsapp-Business-Cloud-Api-Net

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

Can we use this package for our VB.net Desktop application? #27

Closed khaleelanjum closed 1 year ago

khaleelanjum commented 1 year ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

khaleelanjum commented 1 year ago

image

This is what I am facing.

gabrieldwight commented 1 year ago

Hi @khaleelanjum if your desktop application is using the .NET framework target version 4.6 and above the package should work since the minimum version is .NET Standard 2.0.

khaleelanjum commented 1 year ago

Thanks a lot. It helps when i changed .NET Framework to 4.7.2 Now I write the following code but it is not working. Can anyone please take a look

`Imports System.Net.Http Imports System.Threading.Tasks Imports Microsoft.Extensions.DependencyInjection Imports Microsoft.Extensions.Hosting Imports WhatsappBusiness.CloudApi Imports WhatsappBusiness.CloudApi.Configurations Imports WhatsappBusiness.CloudApi.Extensions Imports WhatsappBusiness.CloudApi.Interfaces Imports WhatsappBusiness.CloudApi.Messages.Requests

Public Class WhatsAppBusinessAPI Inherits WhatsAppBusinessCloudApiConfig

Public Shared ServiceProvider As IServiceProvider
Private builder As Object

Public Sub New(ByVal g_WhatsAppBusinessPhoneNumberId As String, ByVal g_WhatsAppBusinessAccountId As String, ByVal g_WhatsAppBusinessId As String, ByVal g_WhatsAppAccessToken As String)
    Dim whatsAppConfig As WhatsAppBusinessCloudApiConfig = New WhatsAppBusinessCloudApiConfig()
    whatsAppConfig.WhatsAppBusinessPhoneNumberId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")(g_WhatsAppBusinessPhoneNumberId)
    whatsAppConfig.WhatsAppBusinessAccountId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")(g_WhatsAppBusinessAccountId)
    whatsAppConfig.WhatsAppBusinessId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")(g_WhatsAppBusinessId)
    whatsAppConfig.AccessToken = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")(g_WhatsAppAccessToken)
    builder.Services.AddWhatsAppBusinessCloudApiService(whatsAppConfig)
End Sub
Private ReadOnly _whatsAppBusinessClient As IWhatsAppBusinessClient

Public Sub New(ByVal whatsAppBusinessClient As IWhatsAppBusinessClient)
    _whatsAppBusinessClient = whatsAppBusinessClient
End Sub

Public Async Function SendTextMessageAsync() As Task(Of String)
    Dim textMessageRequest As TextMessageRequest = New TextMessageRequest()
    textMessageRequest.To = "Recipient Phone Number"
    textMessageRequest.Text = New WhatsAppText()
    textMessageRequest.Text.Body = "Message Body"
    textMessageRequest.Text.PreviewUrl = False
    Dim results = Await _whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest)
End Function

End Class `

EduOrtega commented 1 year ago

Without knowing the context, the class has two constructors, one where you retrieve config parameters and one where you assign the client. In my opinion you should in the first constructor assign the whastapp client as well.

khaleelanjum commented 1 year ago

Thanks for your reply. Actually its a bit confusing as i am not sure where to input config details as documentation is for ASPNETCORE and i am trying in vb.net

gabrieldwight commented 1 year ago

@khaleelanjum if you are not using Dependency injection in VB. You can try this code below to initialize the WhatsApp client:

Private Sub SurroundingSub()
    Dim httpClient = New HttpClient()
    httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress
    Dim whatsAppBusinessClient = New WhatsAppBusinessClient(httpClient, whatsAppConfig)
End Sub

C# equivalent

var httpClient = new HttpClient();

httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress;

//create WhatsAppBusiness API client instance
var whatsAppBusinessClient = new WhatsAppBusinessClient(httpClient, whatsAppConfig);
khaleelanjum commented 1 year ago

Thanks a lot. Following your instruction what I did is

`Imports System.Net.Http Imports System.Threading.Tasks Imports WhatsappBusiness.CloudApi Imports WhatsappBusiness.CloudApi.Configurations Imports WhatsappBusiness.CloudApi.Extensions Imports WhatsappBusiness.CloudApi.Messages.Requests

Public Class WhatsAppBusinessAPI

Private Sub SurroundingSub()
    Dim httpClient = New HttpClient()
    httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress
    Dim whatsAppBusinessClient = New WhatsAppBusinessClient(httpClient, whatsAppConfig)
End Sub`

But in documentations you mentioned to add below code in "ConfigureServices" but here is my case i have no such option where should i input these configurations?

WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig(); whatsAppConfig.WhatsAppBusinessPhoneNumberId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessPhoneNumberId"]; whatsAppConfig.WhatsAppBusinessAccountId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessAccountId"]; whatsAppConfig.WhatsAppBusinessId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessId"]; whatsAppConfig.AccessToken = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AccessToken"]; builder.Services.AddWhatsAppBusinessCloudApiService(whatsAppConfig);

gabrieldwight commented 1 year ago

'Configure services" is used with Microsoft dependency injection container that is applied on aspnetcore project or any project that uses dependency injection from MVVM library.

For your case, you can pass the instantiated WhatsApp configuration object to the second parameter of the whatsappclient constructor. You won't need to use the configure service method.

ctehchid commented 1 year ago

Change builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration") with

Thanks a lot. Following your instruction what I did is

`Imports System.Net.Http Imports System.Threading.Tasks Imports WhatsappBusiness.CloudApi Imports WhatsappBusiness.CloudApi.Configurations Imports WhatsappBusiness.CloudApi.Extensions Imports WhatsappBusiness.CloudApi.Messages.Requests

Public Class WhatsAppBusinessAPI

Private Sub SurroundingSub()
    Dim httpClient = New HttpClient()
    httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress
    Dim whatsAppBusinessClient = New WhatsAppBusinessClient(httpClient, whatsAppConfig)
End Sub`

But in documentations you mentioned to add below code in "ConfigureServices" but here is my case i have no such option where should i input these configurations?

WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig(); whatsAppConfig.WhatsAppBusinessPhoneNumberId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessPhoneNumberId"]; whatsAppConfig.WhatsAppBusinessAccountId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessAccountId"]; whatsAppConfig.WhatsAppBusinessId = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["WhatsAppBusinessId"]; whatsAppConfig.AccessToken = builder.Configuration.GetSection("WhatsAppBusinessCloudApiConfiguration")["AccessToken"]; builder.Services.AddWhatsAppBusinessCloudApiService(whatsAppConfig);

I used this library in C# Desktop app and this library worked perfect. Thanks.

// {WhatsApp XXXXXXXXX} can be look at https://developers.facebook.com/ Look this video when create your whatsapp developer account

WhatsAppBusinessCloudApiConfig whatsAppConfig = new WhatsAppBusinessCloudApiConfig();
whatsAppConfig.WhatsAppBusinessPhoneNumberId = "{ WhatsApp PhoneNumberId }";
whatsAppConfig.WhatsAppBusinessAccountId = "{ WhatsApp ApplicationId }";
whatsAppConfig.WhatsAppBusinessId = "{ WhatsApp BusinessId }";
whatsAppConfig.AccessToken = "{ WhatsApp AccessToken }";

HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = = new Uri("https://graph.facebook.com/v15.0/");;
IWhatsAppBusinessClient waBusinessClient = new WhatsAppBusinessClient(httpClient, whatsAppConfig);

// send text example
ContactMessageRequest contactMessageRequest = new ContactMessageRequest();
contactMessageRequest.To = contactMessageModel.ReceiptPhoneNumber;
contactMessageRequest.Contacts = contactMessageModel.ContactDataList;
WhatsAppResponse results = waBusinessClient.SendContactAttachmentMessage(contactMessageRequest);
khaleelanjum commented 1 year ago

Thanks a lot for this wonderful package. For all other around ..... here is the solution which i am using for my vb.net project

Dim textMessageRequest As TextMessageRequest = New TextMessageRequest()
        textMessageRequest.To = ToMobileNumber.ToString()        '  971544072996
        textMessageRequest.Text = New WhatsAppText()
        textMessageRequest.Text.Body = MessageTextBody.ToString()
        textMessageRequest.Text.PreviewUrl = False
    Dim whatsAppConfig As WhatsAppBusinessCloudApiConfig = New WhatsAppBusinessCloudApiConfig()
    whatsAppConfig.WhatsAppBusinessPhoneNumberId = g_WhatsAppBusinessPhoneNumberId
    whatsAppConfig.WhatsAppBusinessAccountId = g_WhatsAppBusinessAccountId
    whatsAppConfig.WhatsAppBusinessId = g_WhatsAppBusinessId
    whatsAppConfig.AccessToken = g_WhatsAppAccessToken

    Dim httpClient = New HttpClient()
    httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress
    Dim whatsAppBusinessClient = New WhatsAppBusinessClient(httpClient, whatsAppConfig)

    Dim results = Await whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest)`
gabrieldwight commented 1 year ago

Thanks a lot for this wonderful package. For all other around ..... here is the solution which i am using for my vb.net project

Dim textMessageRequest As TextMessageRequest = New TextMessageRequest()
        textMessageRequest.To = ToMobileNumber.ToString()        '  971544072996
        textMessageRequest.Text = New WhatsAppText()
        textMessageRequest.Text.Body = MessageTextBody.ToString()
        textMessageRequest.Text.PreviewUrl = False
    Dim whatsAppConfig As WhatsAppBusinessCloudApiConfig = New WhatsAppBusinessCloudApiConfig()
    whatsAppConfig.WhatsAppBusinessPhoneNumberId = g_WhatsAppBusinessPhoneNumberId
    whatsAppConfig.WhatsAppBusinessAccountId = g_WhatsAppBusinessAccountId
    whatsAppConfig.WhatsAppBusinessId = g_WhatsAppBusinessId
    whatsAppConfig.AccessToken = g_WhatsAppAccessToken

    Dim httpClient = New HttpClient()
    httpClient.BaseAddress = WhatsAppBusinessRequestEndpoint.BaseAddress
    Dim whatsAppBusinessClient = New WhatsAppBusinessClient(httpClient, whatsAppConfig)

    Dim results = Await whatsAppBusinessClient.SendTextMessageAsync(textMessageRequest)`

Good to hear it is working on your side. Any other issue you are experiencing at the moment?

khaleelanjum commented 1 year ago

Not an issue rather a feature request. Is there a way that we can get Whatsapp business API usage like how many messages we have used? and is there any proper response once a message is successfully sent?

gabrieldwight commented 1 year ago

To monitor the number of messages sent from cloud API you can monitor from the developer portal at the moment. Currently, I'm working on the analytics implementation which will have the feature request you stated above. Hopefully, before the year-end, I will push an update.

To monitor whether a message is successfully sent, cloud API sends status updates via webhook subscription. You will need to subscribe your endpoint to the message service which cloud API will send status updates if the message is sent, delivered and read and you will need to use the message id to track the status.

Cloud Api will return this JSON result when sending a message

{
  "messaging_product": "whatsapp",
  "contacts": [
    {
      "input": "48XXXXXXXXX",
      "wa_id": "48XXXXXXXXX "
    }
  ],
  "messages": [
    {
      "id": "wamid.gBGGSFcCNEOPAgkO_KJ55r4w_ww"
    }
  ]
}

Webhook result on message status

{
    "object": "whatsapp_business_account",
    "entry": [
        {
            "id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
            "changes": [
                {
                    "value": {
                        "messaging_product": "whatsapp",
                        "metadata": {
                            "display_phone_number": "PHONE_NUMBER",
                            "phone_number_id": "PHONE_NUMBER_ID"
                        },
                        "statuses": [
                            {
                                "id": "wamid.ID",
                                "recipient_id": "PHONE_NUMBER",
                                "status": "delivered",
                                "timestamp": "TIMESTAMP",
                                "conversation": {
                                    "id": "CONVERSATION_ID",
                                    "expiration_timestamp": TIMESTAMP,
                                    "origin": {
                                        "type": "business_initiated"
                                    }
                                },
                                "pricing": {
                                    "pricing_model": "CBP",
                                    "billable": true
                                }
                            }
                        ]
                    },
                    "field": "messages"
                }
            ]
        }
    ]
}