Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/WhatsApp/MessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ public static TextResponse Reply(this IMessage message, string text)
/// <summary>
/// Creates a text response with buttons for the message.
/// </summary>
public static TextResponse Reply(this IMessage message, string text, Button button1, Button? button2 = default)
public static TextResponse Reply(this IMessage message, string text, Button button1, Button? button2 = default, Button? button3 = default)
=> message is UserMessage user
? new(user.Service, message.UserNumber, message.Id, message.ConversationId, text, button1, button2)
: new(message.ServiceId, message.UserNumber, message.Id, message.ConversationId, text, button1, button2);
? new(user.Service, message.UserNumber, message.Id, message.ConversationId, text, button1, button2, button3)
: new(message.ServiceId, message.UserNumber, message.Id, message.ConversationId, text, button1, button2, button3);

/// <summary>
/// Attempts to retrieve a single message from the specified collection.
Expand Down
10 changes: 6 additions & 4 deletions src/WhatsApp/TextResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
/// <param name="Text">The text content of the response message.</param>
/// <param name="Button1">An optional button to include in the response for user interaction.</param>
/// <param name="Button2">An optional second button to include in the response for user interaction.</param>
public record TextResponse(string ServiceId, string UserNumber, string Context, string? ConversationId, string Text, Button? Button1 = default, Button? Button2 = default) : Response(ServiceId, UserNumber, Context, ConversationId)
public record TextResponse(string ServiceId, string UserNumber, string Context, string? ConversationId, string Text, Button? Button1 = default, Button? Button2 = default, Button? Button3 = default) : Response(ServiceId, UserNumber, Context, ConversationId)
{
// If this variable is not null, it means the originating message came from WhatsApp
// and there's an ongoing conversation with the CLI simultaneously.
readonly CompositeService? service;

internal TextResponse(Service service, string userNumber, string context, string? conversationId, string text, Button? button1 = default, Button? button2 = default)
: this(service.Id, userNumber, context, conversationId, text, button1, button2)
internal TextResponse(Service service, string userNumber, string context, string? conversationId, string text, Button? button1 = default, Button? button2 = default, Button? button3 = default)
: this(service.Id, userNumber, context, conversationId, text, button1, button2, button3)
=> this.service = service as CompositeService;

/// <inheritdoc/>
Expand All @@ -45,8 +45,10 @@ internal TextResponse(Service service, string userNumber, string context, string
{
if (Button2 == null)
return client.ReplyAsync(serviceId, UserNumber, Context, text, Button1, cancellation);
else
else if (Button3 == null)
return client.ReplyAsync(serviceId, UserNumber, Context, text, Button1, Button2, cancellation);
else
return client.ReplyAsync(serviceId, UserNumber, Context, text, Button1, Button2, Button3, cancellation);
}
else
{
Expand Down
45 changes: 45 additions & 0 deletions src/WhatsApp/WhatsAppClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,51 @@ public static Task SendTemplateAsync(this IWhatsAppClient client, string service
}
}, cancellation);

/// <summary>
/// Replies to a user message with a additional interactive buttons.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="serviceId">The service number to send through.</param>
/// <param name="userNumber">The user phone number to send to.</param>
/// <param name="reply">The text message to respond with.</param>
/// <param name="replyTo">The message to reply to.</param>
/// <param name="button1">Interactive button for a user choice.</param>
/// <param name="button2">Interactive button for a user choice.</param>
/// <param name="button3">Interactive button for a user choice.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <returns>The identifier of the reply message.</returns>
/// <see cref="https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages/#interactive-object"/>
public static Task<string?> ReplyAsync(this IWhatsAppClient client, string serviceId, string userNumber, string replyTo, string reply, Button button1, Button button2, Button button3, CancellationToken cancellation = default)
=> client.SendAsync(serviceId, new
{
messaging_product = "whatsapp",
preview_url = false,
recipient_type = "individual",
to = NormalizeNumber(userNumber),
type = "interactive",
context = new
{
message_id = replyTo
},
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 } },
}
}
}
}, cancellation);

/// <summary>
/// Replies to a user message with a additional interactive buttons.
/// </summary>
Expand Down