diff --git a/src/Sample/Program.cs b/src/Sample/Program.cs index 7d957fa..78cd9bb 100644 --- a/src/Sample/Program.cs +++ b/src/Sample/Program.cs @@ -84,7 +84,9 @@ await client.ReactAsync(content, "🧠"); // simulate some hard work at hand, like doing some LLM-stuff :) //await Task.Delay(2000); - await client.ReplyAsync(content, $"☑️ Got your {content.Content.Type}:\r\n{JsonSerializer.Serialize(content, options)}"); + await client.ReplyAsync(content, $"☑️ Got your {content.Content.Type}:\r\n{JsonSerializer.Serialize(content, options)}", + new Button("btn_good", "👍"), + new Button("btn_bad", "👎")); } else if (message is UnsupportedMessage unsupported) { diff --git a/src/WhatsApp/WhatsAppClientExtensions.cs b/src/WhatsApp/WhatsAppClientExtensions.cs index 9a0233b..c7a9123 100644 --- a/src/WhatsApp/WhatsAppClientExtensions.cs +++ b/src/WhatsApp/WhatsAppClientExtensions.cs @@ -79,6 +79,120 @@ public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, } }); + /// + /// Replies to a user message with an additional interactive button. + /// + /// The WhatsApp client. + /// The message to reply to. + /// The text message to respond with. + /// Interactive button for users to reply. + public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button) + => client.SendAsync(message.To.Id, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(message.From.Number), + type = "interactive", + context = new + { + message_id = message.Id + }, + interactive = new + { + type = "button", + body = new + { + text = reply + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button.Id, title = button.Title } }, + } + } + } + }); + + /// + /// Replies to a user message with a additional interactive buttons. + /// + /// The WhatsApp client. + /// The message to reply to. + /// The text message to respond with. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button1, Button button2) + => client.SendAsync(message.To.Id, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(message.From.Number), + type = "interactive", + context = new + { + message_id = message.Id + }, + interactive = new + { + type = "button", + body = new + { + text = reply + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button1.Id, title = button1.Title } }, + new { type = "reply", reply = new { id = button2.Id, title = button2.Title } }, + } + } + } + }); + + /// + /// Replies to a user message with a additional interactive buttons. + /// + /// The WhatsApp client. + /// The message to reply to. + /// The text message to respond with. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button1, Button button2, Button button3) + => client.SendAsync(message.To.Id, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(message.From.Number), + type = "interactive", + context = new + { + message_id = message.Id + }, + interactive = new + { + type = "button", + body = new + { + text = reply + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button1.Id, title = button1.Title } }, + new { type = "reply", reply = new { id = button2.Id, title = button2.Title } }, + new { type = "reply", reply = new { id = button3.Id, title = button3.Title } }, + } + } + } + }); + /// /// Replies to the message a user reacted to. /// @@ -107,8 +221,43 @@ public static Task ReplyAsync(this IWhatsAppClient client, ReactionMessage messa /// Sends a text message a user given his incoming message, without making it a reply. /// /// The WhatsApp client. - public static Task SendAsync(this IWhatsAppClient client, Message message, string text) - => SendAsync(client, message.To.Id, message.From.Number, text); + /// The originating user to send a message to. + /// The text message to send. + public static Task SendAsync(this IWhatsAppClient client, Message to, string message) + => SendAsync(client, to.To.Id, to.From.Number, message); + + /// + /// Sends a text message a user given his incoming message, without making it a reply. + /// + /// The WhatsApp client. + /// The originating user to send a message to. + /// The text message to send. + /// Interactive button for users to reply. + public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button) + => SendAsync(client, to.To.Id, to.From.Number, message, button); + + /// + /// Sends a text message a user given his incoming message, without making it a reply. + /// + /// The WhatsApp client. + /// The originating user to send a message to. + /// The text message to send. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button1, Button button2) + => SendAsync(client, to.To.Id, to.From.Number, message, button1, button2); + + /// + /// Sends a text message a user given his incoming message, without making it a reply. + /// + /// The WhatsApp client. + /// The originating user to send a message to. + /// The text message to send. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button1, Button button2, Button button3) + => SendAsync(client, to.To.Id, to.From.Number, message, button1, button2, button3); /// /// Sends a text message a user. @@ -131,6 +280,111 @@ public static Task SendAsync(this IWhatsAppClient client, string from, string to } }); + /// + /// Sends a text message a user. + /// + /// The WhatsApp client. + /// The service number to send the message through. + /// The user phone number to send the message to. + /// The text message to send. + /// Interactive button for users to reply. + public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button) + => client.SendAsync(from, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(to), + type = "interactive", + interactive = new + { + type = "button", + body = new + { + text = message + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button.Id, title = button.Title } }, + } + } + } + }); + + /// + /// Sends a text message a user. + /// + /// The WhatsApp client. + /// The service number to send the message through. + /// The user phone number to send the message to. + /// The text message to send. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button1, Button button2) + => client.SendAsync(from, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(to), + type = "interactive", + interactive = new + { + type = "button", + body = new + { + text = message + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button1.Id, title = button1.Title } }, + new { type = "reply", reply = new { id = button2.Id, title = button2.Title } }, + } + } + } + }); + + /// + /// Sends a text message a user. + /// + /// The WhatsApp client. + /// The service number to send the message through. + /// The user phone number to send the message to. + /// The text message to send. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + /// Interactive button for a user choice. + public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button1, Button button2, Button button3) + => client.SendAsync(from, new + { + messaging_product = "whatsapp", + preview_url = false, + recipient_type = "individual", + to = NormalizeNumber(to), + type = "interactive", + interactive = new + { + type = "button", + body = new + { + text = message + }, + action = new + { + buttons = new[] + { + new { type = "reply", reply = new { id = button1.Id, title = button1.Title } }, + new { type = "reply", reply = new { id = button2.Id, title = button2.Title } }, + new { type = "reply", reply = new { id = button3.Id, title = button3.Title } }, + } + } + } + }); + static string NormalizeNumber(string number) => // On the web, we don't get the 9 after 54 \o/ // so for Argentina numbers, we need to remove the 9.