From b0fd4973b32d8e585f676d53f30ab05a57ccc0dd Mon Sep 17 00:00:00 2001 From: Neyer Olmos Choque Date: Thu, 9 Sep 2021 10:29:58 -0400 Subject: [PATCH 1/3] WhatsAppPayload updated with the last click to chat feature and link structure to send messages --- QRCoder/PayloadGenerator.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/QRCoder/PayloadGenerator.cs b/QRCoder/PayloadGenerator.cs index 8f3bd0f8..acfe33dd 100644 --- a/QRCoder/PayloadGenerator.cs +++ b/QRCoder/PayloadGenerator.cs @@ -331,7 +331,10 @@ public class WhatsAppMessage : Payload /// /// Let's you compose a WhatApp message and send it the receiver number. /// - /// Receiver phone number + /// Receiver phone number where the 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 + /// /// The message public WhatsAppMessage(string number, string message) { @@ -351,7 +354,7 @@ public WhatsAppMessage(string message) public override string ToString() { - return ($"whatsapp://send?phone={this.number}&text={Uri.EscapeDataString(message)}"); + return ($"https://wa.me/{this.number}?text={Uri.EscapeDataString(message)}"); } } From f75026e335980678b0eb698d923dff3c6ef73e84 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Mon, 1 Nov 2021 12:06:04 +0100 Subject: [PATCH 2/3] Fixed existing Whatsapp test cases --- QRCoderTests/PayloadGeneratorTests.cs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/QRCoderTests/PayloadGeneratorTests.cs b/QRCoderTests/PayloadGeneratorTests.cs index f60da2b7..9766524e 100644 --- a/QRCoderTests/PayloadGeneratorTests.cs +++ b/QRCoderTests/PayloadGeneratorTests.cs @@ -2944,6 +2944,31 @@ public void whatsapp_should_add_unused_params() .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."); } + [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")] @@ -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() From 0c3de71a9459538ca23a8826893aef57243f94bf Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Mon, 1 Nov 2021 12:06:58 +0100 Subject: [PATCH 3/3] Added Whatsapp convenience function (phone cleanup) + tests --- QRCoder/PayloadGenerator.cs | 3 ++- QRCoderTests/PayloadGeneratorTests.cs | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/QRCoder/PayloadGenerator.cs b/QRCoder/PayloadGenerator.cs index 89d6379b..c5df52fc 100644 --- a/QRCoder/PayloadGenerator.cs +++ b/QRCoder/PayloadGenerator.cs @@ -354,7 +354,8 @@ public WhatsAppMessage(string message) public override string ToString() { - return ($"https://wa.me/{this.number}?text={Uri.EscapeDataString(message)}"); + var cleanedPhone = Regex.Replace(this.number, @"^[0+]+|[ ()-]", string.Empty); + return ($"https://wa.me/{cleanedPhone}?text={Uri.EscapeDataString(message)}"); } } diff --git a/QRCoderTests/PayloadGeneratorTests.cs b/QRCoderTests/PayloadGeneratorTests.cs index 9766524e..64717574 100644 --- a/QRCoderTests/PayloadGeneratorTests.cs +++ b/QRCoderTests/PayloadGeneratorTests.cs @@ -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] @@ -2941,7 +2941,7 @@ 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]