![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 1 Crear un chatbot en C](https://www.programadornovato.com/wp-content/uploads/2023/03/Crear-un-chatbot-en-C-640x360.png)
Crear un ChatBot inteligente con WhatsApp en C# (Gratis)
En este tutorial aprenderemos a crear un ChatBot inteligente con WhatsApp en C# totalmente gratis y con la plataforma oficial de WhatsApp (WhatsApp cloud API). La ventaja que tenemos al trabajar con la plataforma oficial es que la informacion que recibiremos y enviaremos será completamente fiable. solo debemos seguir estos pasos:
Creamos una app en Facebook
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 2 Enviar mensajes con Api Oficial de WhatsApp en PHP](https://www.programadornovato.com/wp-content/uploads/2023/01/image-51.png)
O podemos aceder a este link Todas las apps – Meta for Developers (facebook.com)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 3 image 31](https://www.programadornovato.com/wp-content/uploads/2023/01/image-31-1024x361.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 4 image 32](https://www.programadornovato.com/wp-content/uploads/2023/01/image-32.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 5 image 33](https://www.programadornovato.com/wp-content/uploads/2023/01/image-33.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 6 image 34](https://www.programadornovato.com/wp-content/uploads/2023/01/image-34-1024x592.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 7 image 35](https://www.programadornovato.com/wp-content/uploads/2023/01/image-35-1024x699.png)
Configuramos el Api de WhatsApp
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 8 image 36](https://www.programadornovato.com/wp-content/uploads/2023/01/image-36-1024x671.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 9 image 37](https://www.programadornovato.com/wp-content/uploads/2023/01/image-37-1024x673.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 10 image 38](https://www.programadornovato.com/wp-content/uploads/2023/01/image-38-1024x678.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 11 image 49](https://www.programadornovato.com/wp-content/uploads/2023/01/image-49-1024x845.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 12 image 39](https://www.programadornovato.com/wp-content/uploads/2023/01/image-39-1024x748.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 13 image 40](https://www.programadornovato.com/wp-content/uploads/2023/01/image-40-1024x830.png)
Código para enviar mensajes con Api Oficial de WhatsApp en C#
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 14 image](https://www.programadornovato.com/wp-content/uploads/2023/03/image.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 15 image 1](https://www.programadornovato.com/wp-content/uploads/2023/03/image-1.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 16 image 2](https://www.programadornovato.com/wp-content/uploads/2023/03/image-2.png)
Código para recibir mensajes de WhatsApp con C#
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 17 image 22](https://www.programadornovato.com/wp-content/uploads/2023/03/image-22-1024x661.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 18 image 24](https://www.programadornovato.com/wp-content/uploads/2023/03/image-24.png)
//Controllers\RecibirController.cs using ChatBotWa.Models; using Microsoft.AspNetCore.Mvc; using System.IO; using System.Net; using System.Net.Http.Headers; using System.Xml.Linq; namespace ChatBotWa.Controllers { public class RecibirController { //RECIBIMOS LOS DATOS DE VALIDACION VIA GET [HttpGet] //DENTRO DE LA RUTA webhook [Route("webhook")] //RECIBIMOS LOS PARAMETROS QUE NOS ENVIA WHATSAPP PARA VALIDAR NUESTRA URL public string Webhook( [FromQuery(Name = "hub.mode")] string mode, [FromQuery(Name = "hub.challenge")] string challenge, [FromQuery(Name = "hub.verify_token")] string verify_token ) { //SI EL TOKEN ES hola (O EL QUE COLOQUEMOS EN FACEBOOK) if (verify_token.Equals("hola")) { return challenge; } else { return ""; } } //RECIBIMOS LOS DATOS DE VIA POST [HttpPost] //DENTRO DE LA RUTA webhook [Route("webhook")] //RECIBIMOS LOS DATOS Y LOS GUARDAMOS EN EL MODELO WebHookResponseModel public dynamic datos([FromBody] WebHookResponseModel entry) { //ESTRAEMOS EL MENSAJE RECIBIDO string mensaje_recibido = entry.entry[0].changes[0].value.messages[0].text.body; //ESTRAEMOS EL ID UNICO DEL MENSAJE string id_wa = entry.entry[0].changes[0].value.messages[0].id; //ESTRAEMOS EL NUMERO DE TELEFONO DEL CUAL RECIBIMOS EL MENSAJE string telefono_wa = entry.entry[0].changes[0].value.messages[0].from; //CREAMOS EL TEXTO DEL ARCHIVO string texto = "mensaje_recibido="+mensaje_recibido + Environment.NewLine; texto = texto+ "id_wa=" + id_wa + Environment.NewLine; texto = texto + "telefono_wa=" + telefono_wa + Environment.NewLine; File.WriteAllText("texto.txt", texto); //SI NO HAY ERROR RETORNAMOS UN OK var response = new HttpResponseMessage(HttpStatusCode.OK); response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); return response; } } }
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 19 image 6](https://www.programadornovato.com/wp-content/uploads/2023/03/image-6.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 20 image 7](https://www.programadornovato.com/wp-content/uploads/2023/03/image-7.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 21 image 23](https://www.programadornovato.com/wp-content/uploads/2023/03/image-23.png)
//Models\Datos.cs namespace ChatBotWa.Models { public class Datos { } public class WebHookResponseModel { public Entry[] entry { get; set; } } public class Entry { public Change[] changes { get; set; } } public class Change { public Value value { get; set; } } public class Value { public int ad_id { get; set; } public long form_id { get; set; } public long leadgen_id { get; set; } public int created_time { get; set; } public long page_id { get; set; } public int adgroup_id { get; set; } public Messages[] messages { get; set; } } public class Messages { public string id { get; set; } public string from { get; set; } public Text text { get; set; } } public class Text { public string body { get; set; } } }
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 22 image 15](https://www.programadornovato.com/wp-content/uploads/2023/03/image-15-1024x609.png)
Activamos un servidor ngrok
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 23 image 94](https://www.programadornovato.com/wp-content/uploads/2023/01/image-94-1024x514.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 24 image 95](https://www.programadornovato.com/wp-content/uploads/2023/01/image-95-1024x588.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 25 image 97](https://www.programadornovato.com/wp-content/uploads/2023/01/image-97-1024x598.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 26 image 60](https://www.programadornovato.com/wp-content/uploads/2023/01/image-60-1024x530.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 27 Recibir mensajes con Api Oficial de WhatsApp en C#](https://www.programadornovato.com/wp-content/uploads/2023/01/image-61-1024x813.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 28 Enviamos un texto desde Whatsapp y lo podemos leer en el archivo texto que esta en la raiz del proyecto](https://www.programadornovato.com/wp-content/uploads/2023/03/image-25-1024x425.png)
Código para enviar mensajes de WhatsApp con C#
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 29 image 17](https://www.programadornovato.com/wp-content/uploads/2023/03/image-17-1024x681.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 30 image 19](https://www.programadornovato.com/wp-content/uploads/2023/03/image-19-1024x619.png)
//Crear un ChatBot inteligente con WhatsApp en C# //Controllers\EnviarController.cs namespace ChatBotWa.Controllers { public class EnviarController { //RECIBIMOS LOS DATOS DE VALIDACION VIA GET [HttpGet] //DENTRO DE LA RUTA envia [Route("envia")] //RECIBIMOS LOS PARAMETROS QUE NOS ENVIA WHATSAPP PARA VALIDAR NUESTRA URL public async Task enviaAsync() { string token = "EAAWRxZBz2YGQBAGlneZCWwJgwkP2ZBYEk5PoHDL9gsZCgxY3C0IpMTgHHW9cAi5PSApuzRSgUu5flKlTGgZCl1uxL6sjCqixRmkhldhOvVOYYIVOZBOyQ6WV3bm3jZCmWlfsdGb9gU8ZCddrHmFNfJ2FyhjJ43G9VgOtNuVTcZAoZAYoyZA1uYZBZB0ICPwDgDcpGscug9L2USOZAKgAZDZD"; //Identificador de número de teléfono string idTelefono = "111290641852610"; //Nuestro telefono string telefono = "527121122441"; HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://graph.facebook.com/v15.0/" + idTelefono + "/messages"); request.Headers.Add("Authorization", "Bearer " + token); request.Content = new StringContent("{\"messaging_product\": \"whatsapp\",\"recipient_type\": \"individual\",\"to\": \"" + telefono + "\",\"type\": \"text\",\"text\": {\"body\": \"prueba\"}}"); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage response = await client.SendAsync(request); //response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); } } }
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 31 image 18](https://www.programadornovato.com/wp-content/uploads/2023/03/image-18-1024x336.png)
![Crear un ChatBot inteligente con WhatsApp en C# (Gratis) 32 Crear un ChatBot inteligente con WhatsApp en C#](https://www.programadornovato.com/wp-content/uploads/2023/03/image-20-1024x564.png)