Mohamed Kamel
Mohamed Kamel

Reputation: 43

send a WhatsApp message using .Net Maui

is there any way to send a WhatsApp message using .Net Maui I have tried many Api but nothing works for me

Upvotes: 2

Views: 3500

Answers (3)

Gabic
Gabic

Reputation: 704

I remembered that I was able to do this when using Xamarin, so I checked the decompiled file of the Xamarin.Forms.OpenWhatsApp.Chat class and adapted the code to work for MAUI:

public static async void SendWhatsApp(string phoneNumber, string message = null)
{
    try
    {
        string text = "whatsapp://send?phone=" + phoneNumber;
        if (!string.IsNullOrWhiteSpace(message))
        {
            text = text + "&text=" + message;
        }

        await Browser.Default.OpenAsync(new Uri(text), BrowserLaunchMode.External);
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

It worked on my phone (Samsung S10Plus with Android 10). Just remember that:

  1. The device MUST have WhatsApp installed, so it didn't work on Visual Studio emulator
  2. The phone number must be in the correct format, as described in WhatsApp's Help Center: a plus sign (+) followed by the country code, city code, and local phone number.

Upvotes: 1

Fernando Silva
Fernando Silva

Reputation: 111

is very simple! You can use the Launcher documentation.

bool supportsUri = await Launcher.Default.CanOpenAsync($"whatsapp://send?phone=+{phoneNumber}");

if (supportsUri)
    await Launcher.Default.OpenAsync($"whatsapp://send?phone=+{phoneNumber}&text=Hello World!");

else
    await App.Current.MainPage.DisplayAlert("Alert", "Was not possible open the WhatsApp.", "OK");

Upvotes: 3

Konrad Talik
Konrad Talik

Reputation: 916

The only official way of sending a WhatsApp message from one number to another requires WhatsApp Business API. Having that, your company (or your client's) will have an official WhatsApp account that can be used for messaging with consumers.

WhatsApp Business API requires some developer effort, but there are also different WhatsApp API based solutions that make this whole process a lot easier.

One of them is get.chat WhatsApp Business Team Inbox & WhatsApp Integration API which will help you to integrate with WhatsApp much faster. Plus, it will enable access to non-technical people in your team/company, too!

Check this easy guide about WhatsApp Business API, including 2 minute video simple explanation.

I am a co-founder and CTO so I am open to answer any questions if you want to know more about our solution. Here is also a link to the docs if you're interested in learning more by yourself.

Upvotes: 1

Related Questions