在 .netcore 3.1 应用程序中自动发送 Whatsapp 消息

问题描述 投票:0回答:1

我目前正在开展一个项目,目标是集成一项功能,在发生购买时通过 WhatsApp 通知客户。经过在线研究,WhatsApp 似乎没有为此目的提供公共 API。考虑到我们小型且不断发展的组织的资源有限,我正在探索具有成本效益或免费的解决方案。有没有办法在不违反 WhatsApp 条款或产生大量成本的情况下实现此功能?

c# .net automated-tests whatsapp
1个回答
0
投票

首先,您需要一个 Business WhatsApp 帐户

然后您可以使用Meta API

这个是一个使用API的Postman集合

您可以找到通过 WhatsApp API 发送消息的方式

这是上面邮递员集合中的 Curl 示例

curl -i -X POST \
https://graph.facebook.com/{{Version}}/FROM_PHONE_NUMBER_ID/messages \
-H 'Authorization: Bearer ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
-d '{ "messaging_product": "whatsapp", "to": "TO_PHONE_NUMBER", "type": "template", "template": { "name": "hello_world", "language": { "code": "en_US" } } }'

可以像这样在 C# 中使用:

using (var httpClient = new HttpClient())
{
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://graph.facebook.com/{{Version}}/FROM_PHONE_NUMBER_ID/messages"))
    {
        request.Headers.TryAddWithoutValidation("Authorization", "Bearer ACCESS_TOKEN"); 

        request.Content = new StringContent("{ \"messaging_product\": \"whatsapp\", \"to\": \"TO_PHONE_NUMBER\", \"type\": \"template\", \"template\": { \"name\": \"hello_world\", \"language\": { \"code\": \"en_US\" } } }");
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); 

        var response = await httpClient.SendAsync(request);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.