ASP.NET C#中使用WhatsApp API发消息遇WART问题,求实现方案
Hey there, I’ve been through a similar headache trying to integrate WhatsApp into an ASP.NET C# app, so let me share what worked for me and how to get past that WART tool issue.
First off, the WART tool is pretty outdated at this point—WhatsApp has tightened their authentication rules and blocked many of these older unofficial methods. Relying on it isn’t just unreliable, it’s also against WhatsApp’s Terms of Service, which can get your phone number permanently banned. Let’s skip that mess and go with the official, supported approach: the WhatsApp Business Platform.
Head over to the Meta for Developers portal, create a new app, and add the WhatsApp Business product to it. Here’s what you’ll need to do next:
- Verify your business phone number (this will be the number sending messages from your app).
- Grab your Access Token and Phone Number ID from the dashboard—these replace the old WART-generated password entirely.
Here’s a straightforward example using HttpClient to send a text message. I’m using Newtonsoft.Json for serialization, but you can use System.Text.Json too if that’s your preference:
using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; public async Task SendWhatsAppNotification(string recipientNumber, string messageContent) { // Replace these with your actual values from Meta dashboard var accessToken = "YOUR_META_ACCESS_TOKEN"; var phoneNumberId = "YOUR_PHONE_NUMBER_ID"; // Recipient number must include country code (e.g., +1234567890) var recipient = recipientNumber.StartsWith("+") ? recipientNumber : $"+{recipientNumber}"; using var client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var payload = new { messaging_product = "whatsapp", to = recipient, text = new { body = messageContent } }; var jsonPayload = JsonConvert.SerializeObject(payload); var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); var response = await client.PostAsync($"https://graph.facebook.com/v18.0/{phoneNumberId}/messages", content); if (response.IsSuccessStatusCode) { var responseData = await response.Content.ReadAsStringAsync(); // Log success or process the response data Console.WriteLine($"Message sent successfully: {responseData}"); } else { var errorDetails = await response.Content.ReadAsStringAsync(); // Handle errors (e.g., invalid number, quota exceeded) Console.WriteLine($"Failed to send message: {errorDetails}"); } }
If you need to track delivery statuses or receive incoming messages, set up a webhook endpoint in your ASP.NET app. Meta will send POST requests here with event data. Here’s a quick controller example:
[ApiController] [Route("api/whatsapp/webhook")] public class WhatsAppWebhookController : ControllerBase { // This endpoint verifies your webhook with Meta [HttpGet] public IActionResult VerifyWebhook([FromQuery] string hub_mode, [FromQuery] string hub_challenge, [FromQuery] string hub_verify_token) { // Use the same verify token you set in your Meta dashboard var expectedToken = "YOUR_CUSTOM_VERIFY_TOKEN"; if (hub_mode == "subscribe" && hub_verify_token == expectedToken) { return Ok(hub_challenge); } return Forbid(); } // This endpoint receives incoming events (messages, delivery statuses, etc.) [HttpPost] public async Task<IActionResult> ReceiveEvents() { var eventData = await new StreamReader(Request.Body).ReadToEndAsync(); // Parse the JSON data and handle events as needed // Example: Log incoming messages, update delivery statuses in your database return Ok(); } }
If you’re tempted to stick with unofficial tools (like WART or web scraping wrappers), don’t. WhatsApp actively blocks these integrations, so your app will break without warning, and you risk losing access to your business phone number. The official platform is the only sustainable way to integrate WhatsApp into your ASP.NET app.
内容的提问来源于stack exchange,提问作者Sumit Pathak

