Skip to content
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
11 changes: 10 additions & 1 deletion src/WhatsApp/AzureFunctionsConsole.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using System.Text.Json;
using System.Xml;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
Expand All @@ -18,6 +19,12 @@ class AzureFunctionsConsole(
ILogger<AzureFunctionsWebhook> logger,
IHostEnvironment environment)
{
static readonly JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};

[Function("whatsapp_console")]
public async Task<IActionResult> MessageConsole([HttpTrigger(AuthorizationLevel.Anonymous, ["post", "get"], Route = "whatsappcli")] HttpRequest req)
{
Expand Down Expand Up @@ -47,7 +54,9 @@ public async Task<IActionResult> MessageConsole([HttpTrigger(AuthorizationLevel.

using var reader = new StreamReader(req.Body, Encoding.UTF8);
var json = await reader.ReadToEndAsync();
logger.LogDebug("Received WhatsApp message: {Message}.", json);
logger.LogDebug("Received WhatsApp message: {Message}.",
environment.IsProduction() ? json :
JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(json), options));

// Try to deserialize the message sent by the console
if (JsonSerializer.Deserialize(json, JsonContext.Default.Message) is Message message)
Expand Down
15 changes: 13 additions & 2 deletions src/WhatsApp/AzureFunctionsWebhook.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
Expand All @@ -10,6 +11,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using static Microsoft.IO.RecyclableMemoryStreamManager;

namespace Devlooped.WhatsApp;

Expand All @@ -32,14 +34,23 @@ class AzureFunctionsWebhook(
IHostEnvironment hosting,
ILogger<AzureFunctionsWebhook> logger)
{
static readonly JsonSerializerOptions options = new(JsonSerializerDefaults.Web)
{
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true
};

readonly WhatsAppOptions functionOptions = functionOptions.Value;

[Function("whatsapp_message")]
public async Task<IActionResult> Message([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "whatsapp")] HttpRequest req)
{
using var reader = new StreamReader(req.Body, Encoding.UTF8);
var json = await reader.ReadToEndAsync();
logger.LogDebug("Received WhatsApp message: {Message}.", json);

logger.LogDebug("Received WhatsApp message: {Message}.",
hosting.IsProduction() ? json :
JsonSerializer.Serialize(JsonSerializer.Deserialize<JsonElement>(json), options));

// Detect encrypted flow request setup for flows endpoints
if (JsonSerializer.Deserialize<EncryptedFlowData>(json) is { Data.Length: > 0, IV.Length: > 0, Key.Length: > 0 } encrypted)
Expand Down
Loading