using System.Collections.Generic;
using System.Security.Cryptography;
using System.Threading.Tasks;
private const string _messageApiKey = "EvV2tcDuI5I5EYvnk3U6Za45o2pi2kfNY12hCGs331V031i3CG";
private const string _listenerUri = "https://sap-cgi-tc-sbx.com/listener";
private const string _payload = "{\"time_stamp\":\"2020-04-19T23:57:35\",\"message\":\"{\\\"CanRemoteClose\\\":true,\\\"CanRemoteOpen\\\":true,\\\"UserId\\\":\\\"91dc24f8-058f-48db-b82a-7022d63c5dcc\\\",\\\"MyqCorrelationId\\\":\\\"3088c254-7a79-49ae-97d1-5db9d4182629\\\",\\\"SerialNumber\\\":\\\"CG085F00521A\\\",\\\"DeviceType\\\":\\\"door\\\",\\\"DeviceName\\\":\\\"CG085F00521A\\\",\\\"DeviceState\\\":\\\"Closed\\\",\\\"GatewaySerialNumber\\\":\\\"GW0AFF1005C3\\\",\\\"DeviceOnline\\\":true}\",\"message_type\":\"statechangedwithstate\"}";
private const string _eventType = "statechanged";
public static async Task Main(string[] args)
Console.WriteLine("Hello World!");
await NotifyDeviceStateChanged();
public static async Task NotifyDeviceStateChanged()
var headers = new Dictionary<string, string>
{"X-Myq-Event-Type", _eventType },
{"X-Myq-Signature", GetRequestSignature()},
{"User-Agent","MyQ/1.0" }
var webhookmessage = JsonConvert.SerializeObject(new NotificationMessage
TimeStamp = DateTime.Now.ToString(),
await PostRawAsync(headers, webhookmessage);
private static async Task PostRawAsync(Dictionary<string, string> headers, string data)
var request = new HttpRequestMessage(HttpMethod.Post, _listenerUri);
AddRequiredHeaders(request, headers);
request.Content = new StringContent(data, Encoding.UTF8, "application/json");
var content = request.Content.ReadAsStringAsync().Result;
Console.WriteLine("request content is: " + content);
using (var client = new HttpClient())
Console.WriteLine("POST to " + _listenerUri);
var response = await client.SendAsync(request).ConfigureAwait(false);
Console.WriteLine(response.StatusCode);
if (!response.IsSuccessStatusCode)
Console.WriteLine("Failed response on HTTP webhook. Code: " + response.StatusCode);
Console.WriteLine("Successful webhook call. Response HTTP status code: " + response.StatusCode);
Console.WriteLine(" Response HTTP content: " + response.Content.ReadAsStringAsync().Result);
private static string GetRequestSignature()
var hmacKey = Encoding.UTF8.GetBytes(_messageApiKey);
using (var hmac = new HMACSHA1(hmacKey))
var payloadBytes = Encoding.UTF8.GetBytes(_payload);
var hash = hmac.ComputeHash(payloadBytes);
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
Console.WriteLine("GetSignature is: " + result);
private static void AddRequiredHeaders(HttpRequestMessage request, IDictionary<string, string> headers)
foreach (KeyValuePair<string, string> header in headers) request.Headers.Add(header.Key, header.Value);
public class NotificationMessage
[JsonProperty("time_stamp")]
public string TimeStamp { get; set; }
[JsonProperty("message")]
public string Payload { get; set; }
[JsonProperty("message_type")]
public string PayloadType { get; set; }