Skip to content

Commit

Permalink
Merge pull request #333 from codebude/feature/whatsapp-payload-genera…
Browse files Browse the repository at this point in the history
…tor-update

Feature/whatsapp payload generator update
  • Loading branch information
codebude authored Nov 1, 2021
2 parents a52a22c + 0c3de71 commit 30f8a94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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

0 comments on commit 30f8a94

Please sign in to comment.