Created
February 10, 2021 07:28
-
-
Save danielplawgo/533a81afde36dae31d24b354b8e99a4b to your computer and use it in GitHub Desktop.
SendGrid - Webhook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"SendGrid": { | |
"ApiKey": "apikey", | |
"SenderEmail": "[email protected]", | |
"SenderName": "Blog PROGRAMUJE.NET", | |
"TemplateId": "templateid", | |
"WebhooksKey": "publickey" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async Task<IActionResult> ProcessResponse(Response response) | |
{ | |
var responseContent = await response.Body.ReadAsStringAsync(); | |
if (string.IsNullOrEmpty(responseContent) == false) | |
{ | |
return BadRequest(responseContent); | |
} | |
var header = response.Headers.FirstOrDefault(h => h.Key == "X-Message-Id"); | |
var messageId = header.Value?.FirstOrDefault(); | |
_logger.LogInformation($@"MessageId: {messageId}"); | |
return Ok(new | |
{ | |
MessageId = messageId | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendGridConfig | |
{ | |
public string ApiKey { get; set; } | |
public string SenderEmail { get; set; } | |
public string SenderName { get; set; } | |
public string TemplateId { get; set; } | |
public string WebhooksKey { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
[ApiController] | |
public class SendGridWebhooksController : ControllerBase | |
{ | |
private readonly SendGridConfig _config; | |
private readonly ILogger<SendGridWebhooksController> _logger; | |
public SendGridWebhooksController(IOptions<SendGridConfig> config, | |
ILogger<SendGridWebhooksController> logger) | |
{ | |
_logger = logger; | |
_config = config.Value; | |
} | |
[HttpPost] | |
[Route("InboundEmail")] | |
public async Task<IActionResult> ReceiveInboundEmail() | |
{ | |
try | |
{ | |
var signature = Request.Headers[WebhookParser.SIGNATURE_HEADER_NAME]; | |
var timestamp = Request.Headers[WebhookParser.TIMESTAMP_HEADER_NAME]; | |
var parser = new WebhookParser(); | |
var events = await parser.ParseSignedEventsWebhookAsync(Request.Body, _config.WebhooksKey, signature, timestamp); | |
_logger.LogInformation(JsonConvert.SerializeObject(events)); | |
return Ok(); | |
} | |
catch (SecurityException e) | |
{ | |
return BadRequest(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment