Skip to content

Commit 08d0d9b

Browse files
committed
Add support for contacts' formatted name
In addition to first_name and last_name, sometimes the formatted_name provides the otherwise missing last_name. If it's provided, we consider it to supply the misisng Name/Surname as needed.
1 parent e0732cc commit 08d0d9b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/WhatsApp/Content.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
44
using Microsoft.Extensions.AI;
5+
using Microsoft.Identity.Client;
56

67
namespace Devlooped.WhatsApp;
78

@@ -52,8 +53,31 @@ public record ContactsContent(Contact[] Contacts) : Content
5253
/// <summary>
5354
/// Contact information.
5455
/// </summary>
55-
public record Contact(string Name, string Surname, string[] Numbers)
56+
public class Contact(string name, string surname, string[] numbers)
5657
{
58+
public string Name { get; set; } = name;
59+
public string Surname { get; set; } = surname;
60+
public string[] Numbers => numbers;
61+
[JsonPropertyName("fullname")]
62+
public string FullName
63+
{
64+
get => string.IsNullOrWhiteSpace(Surname) ? Name : $"{Name} {Surname}";
65+
set
66+
{
67+
if (!string.IsNullOrEmpty(value))
68+
{
69+
var parts = value.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
70+
if (parts.Length >= 2)
71+
{
72+
if (string.IsNullOrEmpty(Name))
73+
Name = parts[0];
74+
if (string.IsNullOrEmpty(Surname))
75+
Surname = string.Join(' ', parts[1..]);
76+
}
77+
}
78+
}
79+
}
80+
5781
public override string ToString() => $"{Name} {Surname} ({string.Join(", ", Numbers)})";
5882
}
5983

src/WhatsApp/Message.jq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"contacts": $msg.contacts | map({
9898
"name": .name.first_name,
9999
"surname": .name.last_name,
100+
"fullname": .name.formatted_name,
100101
"numbers": [.phones[] | select(.wa_id? != null) | .wa_id]
101102
})
102103
}

0 commit comments

Comments
 (0)