Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/whatsapp payload generator update #333

Merged
merged 4 commits into from
Nov 1, 2021
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
8 changes: 6 additions & 2 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ public class WhatsAppMessage : Payload
/// <summary>
/// Let's you compose a WhatApp message and send it the receiver number.
/// </summary>
/// <param name="number">Receiver phone number</param>
/// <param name="number">Receiver phone number where the <number> is a full phone number in international format.
/// Omit any zeroes, brackets, or dashes when adding the phone number in international format.
/// Use: 1XXXXXXXXXX | Don't use: +001-(XXX)XXXXXXX
/// </param>
/// <param name="message">The message</param>
public WhatsAppMessage(string number, string message)
{
Expand All @@ -351,7 +354,8 @@ public WhatsAppMessage(string message)

public override string ToString()
{
return ($"whatsapp://send?phone={this.number}&text={Uri.EscapeDataString(message)}");
var cleanedPhone = Regex.Replace(this.number, @"^[0+]+|[ ()-]", string.Empty);
return ($"https://wa.me/{cleanedPhone}?text={Uri.EscapeDataString(message)}");
}
}

Expand Down
32 changes: 28 additions & 4 deletions QRCoderTests/PayloadGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2923,13 +2923,13 @@ public void contactdata_generator_can_generate_payload_full_vcard4_reverse()
[Category("PayloadGenerator/WhatsAppMessage")]
public void whatsapp_generator_can_generate_payload_simple()
{
var number = "01601234567";
var number = "491601234567";
var msg = "This is a sample message with Umlauts: Ä,ö, ü and ß.";
var generator = new PayloadGenerator.WhatsAppMessage(number, msg);

generator
.ToString()
.ShouldBe("whatsapp://send?phone=01601234567&text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
.ShouldBe("https://wa.me/491601234567?text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
}

[Fact]
Expand All @@ -2941,9 +2941,34 @@ public void whatsapp_should_add_unused_params()

generator
.ToString()
.ShouldBe("whatsapp://send?phone=&text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
.ShouldBe("https://wa.me/?text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
}

[Fact]
[Category("PayloadGenerator/WhatsAppMessage")]
public void whatsapp_should_cleanup_phonenumber_1()
{
var number = "+49(160)1234567";
var msg = "This is a sample message with Umlauts: Ä,ö, ü and ß.";
var generator = new PayloadGenerator.WhatsAppMessage(number, msg);

generator
.ToString()
.ShouldBe("https://wa.me/491601234567?text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
}

[Fact]
[Category("PayloadGenerator/WhatsAppMessage")]
public void whatsapp_should_cleanup_phonenumber_2()
{
var number = "0049-160-1234 567";
var msg = "This is a sample message with Umlauts: Ä,ö, ü and ß.";
var generator = new PayloadGenerator.WhatsAppMessage(number, msg);

generator
.ToString()
.ShouldBe("https://wa.me/491601234567?text=This%20is%20a%20sample%20message%20with%20Umlauts%3A%20%C3%84%2C%C3%B6%2C%20%C3%BC%20and%20%C3%9F.");
}

[Fact]
[Category("PayloadGenerator/Monero")]
Expand All @@ -2957,7 +2982,6 @@ public void monero_generator_can_generate_payload_simple()
.ShouldBe("monero://46BeWrHpwXmHDpDEUmZBWZfoQpdc6HaERCNmx1pEYL2rAcuwufPN9rXHHtyUA4QVy66qeFQkn6sfK8aHYjA3jk3o1Bv16em");
}


[Fact]
[Category("PayloadGenerator/Monero")]
public void monero_generator_can_generate_payload_first_param()
Expand Down