Skip to content

Commit 788c910

Browse files
committed
Add overloads of Reply and Send with interactive buttons
This makes it easier to send interactive responses, which is quite a common scenario.
1 parent 802c7f3 commit 788c910

File tree

2 files changed

+259
-3
lines changed

2 files changed

+259
-3
lines changed

src/Sample/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
await client.ReactAsync(content, "🧠");
8585
// simulate some hard work at hand, like doing some LLM-stuff :)
8686
//await Task.Delay(2000);
87-
await client.ReplyAsync(content, $"☑️ Got your {content.Content.Type}:\r\n{JsonSerializer.Serialize(content, options)}");
87+
await client.ReplyAsync(content, $"☑️ Got your {content.Content.Type}:\r\n{JsonSerializer.Serialize(content, options)}",
88+
new Button("btn_good", "👍"),
89+
new Button("btn_bad", "👎"));
8890
}
8991
else if (message is UnsupportedMessage unsupported)
9092
{

src/WhatsApp/WhatsAppClientExtensions.cs

Lines changed: 256 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,120 @@ public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message,
7979
}
8080
});
8181

82+
/// <summary>
83+
/// Replies to a user message with an additional interactive button.
84+
/// </summary>
85+
/// <param name="client">The WhatsApp client.</param>
86+
/// <param name="message">The message to reply to.</param>
87+
/// <param name="reply">The text message to respond with.</param>
88+
/// <param name="button">Interactive button for users to reply.</param>
89+
public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button)
90+
=> client.SendAsync(message.To.Id, new
91+
{
92+
messaging_product = "whatsapp",
93+
preview_url = false,
94+
recipient_type = "individual",
95+
to = NormalizeNumber(message.From.Number),
96+
type = "interactive",
97+
context = new
98+
{
99+
message_id = message.Id
100+
},
101+
interactive = new
102+
{
103+
type = "button",
104+
body = new
105+
{
106+
text = reply
107+
},
108+
action = new
109+
{
110+
buttons = new[]
111+
{
112+
new { type = "reply", reply = new { id = button.Id, title = button.Title } },
113+
}
114+
}
115+
}
116+
});
117+
118+
/// <summary>
119+
/// Replies to a user message with a additional interactive buttons.
120+
/// </summary>
121+
/// <param name="client">The WhatsApp client.</param>
122+
/// <param name="message">The message to reply to.</param>
123+
/// <param name="reply">The text message to respond with.</param>
124+
/// <param name="button1">Interactive button for a user choice.</param>
125+
/// <param name="button2">Interactive button for a user choice.</param>
126+
public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button1, Button button2)
127+
=> client.SendAsync(message.To.Id, new
128+
{
129+
messaging_product = "whatsapp",
130+
preview_url = false,
131+
recipient_type = "individual",
132+
to = NormalizeNumber(message.From.Number),
133+
type = "interactive",
134+
context = new
135+
{
136+
message_id = message.Id
137+
},
138+
interactive = new
139+
{
140+
type = "button",
141+
body = new
142+
{
143+
text = reply
144+
},
145+
action = new
146+
{
147+
buttons = new[]
148+
{
149+
new { type = "reply", reply = new { id = button1.Id, title = button1.Title } },
150+
new { type = "reply", reply = new { id = button2.Id, title = button2.Title } },
151+
}
152+
}
153+
}
154+
});
155+
156+
/// <summary>
157+
/// Replies to a user message with a additional interactive buttons.
158+
/// </summary>
159+
/// <param name="client">The WhatsApp client.</param>
160+
/// <param name="message">The message to reply to.</param>
161+
/// <param name="reply">The text message to respond with.</param>
162+
/// <param name="button1">Interactive button for a user choice.</param>
163+
/// <param name="button2">Interactive button for a user choice.</param>
164+
/// <param name="button3">Interactive button for a user choice.</param>
165+
public static Task ReplyAsync(this IWhatsAppClient client, UserMessage message, string reply, Button button1, Button button2, Button button3)
166+
=> client.SendAsync(message.To.Id, new
167+
{
168+
messaging_product = "whatsapp",
169+
preview_url = false,
170+
recipient_type = "individual",
171+
to = NormalizeNumber(message.From.Number),
172+
type = "interactive",
173+
context = new
174+
{
175+
message_id = message.Id
176+
},
177+
interactive = new
178+
{
179+
type = "button",
180+
body = new
181+
{
182+
text = reply
183+
},
184+
action = new
185+
{
186+
buttons = new[]
187+
{
188+
new { type = "reply", reply = new { id = button1.Id, title = button1.Title } },
189+
new { type = "reply", reply = new { id = button2.Id, title = button2.Title } },
190+
new { type = "reply", reply = new { id = button3.Id, title = button3.Title } },
191+
}
192+
}
193+
}
194+
});
195+
82196
/// <summary>
83197
/// Replies to the message a user reacted to.
84198
/// </summary>
@@ -107,8 +221,43 @@ public static Task ReplyAsync(this IWhatsAppClient client, ReactionMessage messa
107221
/// Sends a text message a user given his incoming message, without making it a reply.
108222
/// </summary>
109223
/// <param name="client">The WhatsApp client.</param>
110-
public static Task SendAsync(this IWhatsAppClient client, Message message, string text)
111-
=> SendAsync(client, message.To.Id, message.From.Number, text);
224+
/// <param name="to">The originating user to send a message to.</param>
225+
/// <param name="message">The text message to send.</param>
226+
public static Task SendAsync(this IWhatsAppClient client, Message to, string message)
227+
=> SendAsync(client, to.To.Id, to.From.Number, message);
228+
229+
/// <summary>
230+
/// Sends a text message a user given his incoming message, without making it a reply.
231+
/// </summary>
232+
/// <param name="client">The WhatsApp client.</param>
233+
/// <param name="to">The originating user to send a message to.</param>
234+
/// <param name="message">The text message to send.</param>
235+
/// <param name="button">Interactive button for users to reply.</param>
236+
public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button)
237+
=> SendAsync(client, to.To.Id, to.From.Number, message, button);
238+
239+
/// <summary>
240+
/// Sends a text message a user given his incoming message, without making it a reply.
241+
/// </summary>
242+
/// <param name="client">The WhatsApp client.</param>
243+
/// <param name="to">The originating user to send a message to.</param>
244+
/// <param name="message">The text message to send.</param>
245+
/// <param name="button1">Interactive button for a user choice.</param>
246+
/// <param name="button2">Interactive button for a user choice.</param>
247+
public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button1, Button button2)
248+
=> SendAsync(client, to.To.Id, to.From.Number, message, button1, button2);
249+
250+
/// <summary>
251+
/// Sends a text message a user given his incoming message, without making it a reply.
252+
/// </summary>
253+
/// <param name="client">The WhatsApp client.</param>
254+
/// <param name="to">The originating user to send a message to.</param>
255+
/// <param name="message">The text message to send.</param>
256+
/// <param name="button1">Interactive button for a user choice.</param>
257+
/// <param name="button2">Interactive button for a user choice.</param>
258+
/// <param name="button3">Interactive button for a user choice.</param>
259+
public static Task SendAsync(this IWhatsAppClient client, Message to, string message, Button button1, Button button2, Button button3)
260+
=> SendAsync(client, to.To.Id, to.From.Number, message, button1, button2, button3);
112261

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

283+
/// <summary>
284+
/// Sends a text message a user.
285+
/// </summary>
286+
/// <param name="client">The WhatsApp client.</param>
287+
/// <param name="from">The service number to send the message through.</param>
288+
/// <param name="to">The user phone number to send the message to.</param>
289+
/// <param name="message">The text message to send.</param>
290+
/// <param name="button">Interactive button for users to reply.</param>
291+
public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button)
292+
=> client.SendAsync(from, new
293+
{
294+
messaging_product = "whatsapp",
295+
preview_url = false,
296+
recipient_type = "individual",
297+
to = NormalizeNumber(to),
298+
type = "interactive",
299+
interactive = new
300+
{
301+
type = "button",
302+
body = new
303+
{
304+
text = message
305+
},
306+
action = new
307+
{
308+
buttons = new[]
309+
{
310+
new { type = "reply", reply = new { id = button.Id, title = button.Title } },
311+
}
312+
}
313+
}
314+
});
315+
316+
/// <summary>
317+
/// Sends a text message a user.
318+
/// </summary>
319+
/// <param name="client">The WhatsApp client.</param>
320+
/// <param name="from">The service number to send the message through.</param>
321+
/// <param name="to">The user phone number to send the message to.</param>
322+
/// <param name="message">The text message to send.</param>
323+
/// <param name="button1">Interactive button for a user choice.</param>
324+
/// <param name="button2">Interactive button for a user choice.</param>
325+
public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button1, Button button2)
326+
=> client.SendAsync(from, new
327+
{
328+
messaging_product = "whatsapp",
329+
preview_url = false,
330+
recipient_type = "individual",
331+
to = NormalizeNumber(to),
332+
type = "interactive",
333+
interactive = new
334+
{
335+
type = "button",
336+
body = new
337+
{
338+
text = message
339+
},
340+
action = new
341+
{
342+
buttons = new[]
343+
{
344+
new { type = "reply", reply = new { id = button1.Id, title = button1.Title } },
345+
new { type = "reply", reply = new { id = button2.Id, title = button2.Title } },
346+
}
347+
}
348+
}
349+
});
350+
351+
/// <summary>
352+
/// Sends a text message a user.
353+
/// </summary>
354+
/// <param name="client">The WhatsApp client.</param>
355+
/// <param name="from">The service number to send the message through.</param>
356+
/// <param name="to">The user phone number to send the message to.</param>
357+
/// <param name="message">The text message to send.</param>
358+
/// <param name="button1">Interactive button for a user choice.</param>
359+
/// <param name="button2">Interactive button for a user choice.</param>
360+
/// <param name="button3">Interactive button for a user choice.</param>
361+
public static Task SendAsync(this IWhatsAppClient client, string from, string to, string message, Button button1, Button button2, Button button3)
362+
=> client.SendAsync(from, new
363+
{
364+
messaging_product = "whatsapp",
365+
preview_url = false,
366+
recipient_type = "individual",
367+
to = NormalizeNumber(to),
368+
type = "interactive",
369+
interactive = new
370+
{
371+
type = "button",
372+
body = new
373+
{
374+
text = message
375+
},
376+
action = new
377+
{
378+
buttons = new[]
379+
{
380+
new { type = "reply", reply = new { id = button1.Id, title = button1.Title } },
381+
new { type = "reply", reply = new { id = button2.Id, title = button2.Title } },
382+
new { type = "reply", reply = new { id = button3.Id, title = button3.Title } },
383+
}
384+
}
385+
}
386+
});
387+
134388
static string NormalizeNumber(string number) =>
135389
// On the web, we don't get the 9 after 54 \o/
136390
// so for Argentina numbers, we need to remove the 9.

0 commit comments

Comments
 (0)