Hi everyone,
I'm trying to use a Webhook alert to trigger and send a async message to a device using an Rest API. The serial number is in the webhook body and that should be used at the end of the URL, but in the coding this is not working:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
using RestSharp;
using System.Threading;
namespace RTCWebhooks
{
public static class RTCWebhooks
{
[FunctionName("RTCWebhooks")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string requestBody = String.Empty;
using (StreamReader streamReader = new StreamReader(req.Body))
{
requestBody = await streamReader.ReadToEndAsync();
}
dynamic data = JsonConvert.DeserializeObject(requestBody);
string name = req.Query["Asset.DeviceSerial"];
name ??= data?.name;
Console.WriteLine(name);
var client = new RestClient("https://api.oemserver.com")
{
Timeout = -1
};
Console.WriteLine(client);
var request = new RestRequest("/v1.0/asyncmessaging/send/" + name,Method.POST);
//if (name == null)
//{
// name = "223670";
//}
request.AddParameter("name", name, ParameterType.UrlSegment);
request.AddHeader("Authorization", "Basic Secret");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Accept", "application/json");
var body = "{\"MessageType\":256,\"CANAddress\":4294967295,\"Flags\":1,\"Data\":[1],\"ExpiryDateUTC\":\"2024-07-20T18:25:43.511Z\"}";
//var body = new { MessageType = 256, CANAddress = 4294967295, Flags = 1, Data = "[0]", ExpiryDateUTC = "2025-07-20T18:25:43.511Z"};
Console.WriteLine(body);
request.AddJsonBody(body);
//request.AddParameter("application/json", body, ParameterType.RequestBody
IRestResponse response = client.Execute(request);
string fullUrl = client.BuildUri(request).ToString();
Console.WriteLine(fullUrl);
Console.WriteLine("myresponse " + response);
return new OkObjectResult(response.Content);
}
}
}