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
4 changes: 3 additions & 1 deletion src/Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
258 changes: 256 additions & 2 deletions src/WhatsApp/WhatsAppClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,120 @@ public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message,
}
});

/// <summary>
/// Replies to a user message with an additional interactive button.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="message">The message to reply to.</param>
/// <param name="reply">The text message to respond with.</param>
/// <param name="button">Interactive button for users to reply.</param>
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 } },
}
}
}
});

/// <summary>
/// Replies to a user message with a additional interactive buttons.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="message">The message to reply to.</param>
/// <param name="reply">The text message to respond with.</param>
/// <param name="button1">Interactive button for a user choice.</param>
/// <param name="button2">Interactive button for a user choice.</param>
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 } },
}
}
}
});

/// <summary>
/// Replies to a user message with a additional interactive buttons.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="message">The message to reply to.</param>
/// <param name="reply">The text message to respond with.</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>
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 } },
}
}
}
});

/// <summary>
/// Replies to the message a user reacted to.
/// </summary>
Expand Down Expand Up @@ -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.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
public static Task SendAsync(this IWhatsAppClient client, Message message, string text)
=> SendAsync(client, message.To.Id, message.From.Number, text);
/// <param name="to">The originating user to send a message to.</param>
/// <param name="message">The text message to send.</param>
public static Task SendAsync(this IWhatsAppClient client, Message to, string message)
=> SendAsync(client, to.To.Id, to.From.Number, message);

/// <summary>
/// Sends a text message a user given his incoming message, without making it a reply.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="to">The originating user to send a message to.</param>
/// <param name="message">The text message to send.</param>
/// <param name="button">Interactive button for users to reply.</param>
public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button)
=> SendAsync(client, to.To.Id, to.From.Number, message, button);

/// <summary>
/// Sends a text message a user given his incoming message, without making it a reply.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="to">The originating user to send a message to.</param>
/// <param name="message">The text message to send.</param>
/// <param name="button1">Interactive button for a user choice.</param>
/// <param name="button2">Interactive button for a user choice.</param>
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);

/// <summary>
/// Sends a text message a user given his incoming message, without making it a reply.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="to">The originating user to send a message to.</param>
/// <param name="message">The text message to send.</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>
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);

/// <summary>
/// Sends a text message a user.
Expand All @@ -131,6 +280,111 @@ public static Task SendAsync(this IWhatsAppClient client, string from, string to
}
});

/// <summary>
/// Sends a text message a user.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="from">The service number to send the message through.</param>
/// <param name="to">The user phone number to send the message to.</param>
/// <param name="message">The text message to send.</param>
/// <param name="button">Interactive button for users to reply.</param>
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 } },
}
}
}
});

/// <summary>
/// Sends a text message a user.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="from">The service number to send the message through.</param>
/// <param name="to">The user phone number to send the message to.</param>
/// <param name="message">The text message to send.</param>
/// <param name="button1">Interactive button for a user choice.</param>
/// <param name="button2">Interactive button for a user choice.</param>
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 } },
}
}
}
});

/// <summary>
/// Sends a text message a user.
/// </summary>
/// <param name="client">The WhatsApp client.</param>
/// <param name="from">The service number to send the message through.</param>
/// <param name="to">The user phone number to send the message to.</param>
/// <param name="message">The text message to send.</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>
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.
Expand Down