How do I create a webhook for sendgrid in c#? - c#

I am new to using Sendgrid emails using c# .Net library. Our requirements wants us to track the status of the email like Delivered/Went to Spam/Client opened/reported as spam etc., By looking at the documentations and answers from other users to my previous questions its my understanding that there is no direct way to track the status of the email (like result object).
It would be really helpful if someone can point me to some example/sample codes or documentation/implementation in C# for the following
1) Adding unique parameters while sending the email using send grid API. Can I use a Guid string as my argument
I am assuming what I am doing below is correct.
var myMessage = new SendGridMessage();
var identifiers = new Dictionary<String, String>();
identifiers["Email_ID"] = "Email_ID";
identifiers["Email_Key"] = "9ebccd0d-67c0-4c28-bbf3-83d5bb69f098";
myMessage.AddUniqueArgs(identifiers);
2) How to use event webhooks to get the status with the unique argument that I used above from the http_post so that I can associate an email to the status. Any sample code , documentation in c# or an overall idea of how this works will get me started on this.
Appreciate your time and answers.

Sending emails via SendGrid is easier from C# using the official library that SendGrid provides. From your code example, it looks like you may already be using this - good job.
The unique argument should work as long as its been stringified, and you're not trying to pass an object to myMessage.AddUniqueArgs.
The Event Webhook will send a JSON packet to any URL that you specify. If you have included unique arguments in an email that you send out via SendGrid then these are automatically added to each event response you get back from the webhook - you don't need to turn anything else on to get the arguments as well.
There is an example of this call and the resulting response in the SendGrid Documentation.

SendGrid has an Event Webhook which posts events related to your email
activity to a URL of your choice. This is an easily deployable
solution that allows for customers to easiy get up and running
processing (parse and save) their event webhooks.
This is docker-based solution which can be deployed on cloud services
like Heroku out of the box.
https://github.com/sendgrid/sendgrid-csharp/tree/main/examples/eventwebhook/consumer

Related

how to add attributes (application-specific data) to a Twilio SMS - 2021

I am working on an appointment confirmation system where I need to bind the SMS response 'Y' or 'N' to some meta data and send that meta data to my API via the Twilio webhook.
I am using.NET SDK from Twilio and trying to add attributes to the SMS message (MessageResource) which their documentation states:
Attributes:
STRING PII MTL: 30 DAYS
"The JSON string that stores application-specific data. If attributes have not been set, {} is returned."
link to docs: https://www.twilio.com/docs/chat/rest/message-resource#message-properties
which is more like metadata which is what I need but when I try finding any 'attributes' property in my C# code with their SDK I can't find it.
Here's my code:
var twilioMessage = MessageResource.Create(
body: msg,
from: _configuration["Twilio:FromPhoneNo"],
to: new PhoneNumber(phoneNumber),
attempt:1,
attributes: "{"id":"test"}" //this property doesn't work
);
var attributes = twilioMessage.attributes; //this doesn't work either
Any help will be highly appreciated.
Twilio developer evangelist here.
I'm afraid you are looking at the wrong documentation for sending SMS messages there. This documentation is for sending a chat (app to app) message. The documentation for sending an SMS is here.
Sadly, SMS messages do not have the ability to bind custom attributes to them. It is simply something that doesn't exist in the protocol.
You are sending an appointment confirmation and expecting a "Y" or "N" response. I am guessing you are trying to add the ID of the appointment in the case that you send confirmations for more than one appointment to the same phone number and you need to work out which appointment the response is about. There's a further issue here in that, as an end user, you cannot reply to a specific SMS messages. SMS is purely chronological.
There are 3 ways I can think of to work around this:
Only send one confirmation at a time. If you are still waiting for a response to a confirmation, don't send another one until you get that response. This might not work if users don't respond to confirmations.
Have the user include the ID of the appointment in the response. This is flaky because it relies on the users getting things right and you want to make things as simple as you can for them
When you need to send more than one confirmation at a time, use different Twilio numbers to send the confirmations. Then you can tie the confirmation response to the appointment by inspecting the Twilio number that the user sent the message to.
Number 3 is the most sound way of doing this. It shouldn't require you to have too many extra numbers, just as many as you might have concurrent appointment confirmations.
Let me know if this helps at all.

Twilio MessagingResponse not working with csharp DLL 5.16.2

I recently upgraded the Twilio C# DLL we've been using to handle SMS messages. We are now on Twilio.DLL v5.16.2. I have a webhook set up to call a custom URL on our website. That part is working, because I can log the Request coming in (message SID and body and all that good stuff). At the end, we have code to instantiate a MessagingResponse object to send back a confirmation that the message was received. That's not working anymore. I don't get a SMS message on my phone nor do I see an Outgoing API record on the SMS logs page when I'm logged into my project on Twilio.com (whereas I do see the test message I've sent to our number as an Incoming message).
There is no error or exception either.
Any idea how to debug/solve this?
string responseTxt = "TESTING: Got it. Thanks.";
var smsResponse = new MessagingResponse();
var smsMsg = smsResponse.Message(responseTxt);
response.Write(smsMsg);
Twilio developer evangelist here.
I'm not a C# developer, so forgive me if I'm wrong. Also, I don't know what happened with this change.
Anyway, from what I can see, the message body when using the MessagingResponse's Message method appears to be a named argument now. Changing your code to the following should work for the latest version of the DLL.
var smsMsg = smsResponse.Message(body: responseTxt);
Let me know if that helps.
I'm not sure there's much to update as the exact same scenario still stands. But I can reiterate to see if this provides any additional clues.
We have a Twilio SMS service set up. There is a webhook assigned to our number that maps to an HTTP Handler on an ASP.NET website. The code in the handler logs in our database the fact that an SMS message was received and various bits of info (from the Request.Form variables supplied by Twilio, From number, body of the message, num of media, etc.).
Then, the code "replies" to the original SMS sender using the MessagingResponse object. See original code snippet. Really couldn't be much simpler.
Note, this is and has all been working quite well, using the Twilio c# helper library 5.6.
I upgraded the Twilio library to the latest rev (5.25.1) and its dependencies. Copied the new DLLs to our dev webserver. Now the reply message, using the MessagingResponse object, never occurs. The webhook is still called as I can see the new record in the database logging the message details. Just no response. No error, no exception. Just nothing.
If I rollback to our old version of the Twilio library (and its dependencies, thank goodness for Git!), it works again.

SendGridApiClient get delivery response

Just started working on email sending implementation using SendGridApiClient. Have this line that sends an email
dynamic response = await _sendGrid.client.mail.send.post(requestBody: mail.Get());
Response can provide StatusCode Accepted and nothing more. Was wondering how can I check was email delivered or stuck ?
The SendGrid API is asynchronous because the length of time it takes to process delivery of the email is non-trivial and dependent on factors like the receiving server.
The best way to keep an eye on events like delivered, bounced, etc in real-time is to implement the Event Webhook.
Take a look at this answer: Can my ASP.Net Code get confirmation from sendgrid that an email has been sent?

How do you send multiple response to user from the bot?

I am using Microsoft Bot Framework and trying to send multiple responses to user but not sure how to do that from nodejs. I am able to do that from C#.
C# code example
var connector = new ConnectorClient();
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 1.", "en"));
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 2.", "en"));
connector.Messages.SendMessage(incomingMessage.CreateReplyMessage("Yo, I heard you 3.", "en"));
What would this be in nodejs?
nodejs code example that I'm trying to run:
var bot = new builder.TextBot();
bot.add('/', function (session) {
session.send('Hello World');
session.send('Hello World'); // won't work.
});
bot.listenStdin();
Checkout this answer: send multiple responses to client via nodejs
You may want simulate the multiple responses(solution 1) that you want by returning an array property with the response object. In this case it might be helpful using this lib: https://github.com/caolan/async if you are using multiple asynchronous functions.
var messages = [];
messages.push('message 1');
messages.push('message 2');
// etc
response.send({all: messages});
However if you are not able to so so I would recommend the solution 3 since it will keep your code cleaner & most complex web apps use websockets anyway for making the website to not require refreshed for any new data that are needed to be retrieved.
A commonly used library for websockets is socketio: http://socket.io/get-started/chat/
I would not mess with solution 2 unless the functionality is super complicated and requires acknowledgement from the client as well. In that case you could still use solution 3.

DocuSign Envelope Notification

I have successfully integrated my company's system with DocuSign using DocuSign's SOAP API. I can send, check status and retrieve Envelopes through the SOAP interface.
I have read that the preferred method of getting Envelope status is through an event. Unfortunately I haven't had any luck finding an example of this.
I found some documentation about it HERE.
Has anyone used this way of event / notification from DocuSign that would help point me in the right direction?
There are examples of it on DocuSign's own Lithium forums (which will be made read-only soon) in PHP for example. They're pretty easy to setup, you just need a server listening for the events with the rights ports open and you just add the eventNotification element to your request. You've referenced the SOAP api guide, which the sample PHP code below shows how to implement. There's also a version available for REST API.
You can download DocuSign's SOAP SDK out of GitHub and there's sample PHP project ready of out of the box for you to start modifying and adding in eventNotifications.
// Notifications
$eventNoti = new EventNotification();
$eventNoti->URL = 'http://myurl.com/docusign/updateDocStatus'.$env_id.'/';
$eventNoti->LoggingEnabled = "TRUE";
// Important Stuff below
$envEvent = new EnvelopeEvent();
$envEvent->EnvelopeEventStatusCode = "Completed"; // <---------- Fires on "Completed" only
$envEvent->IncludeDocuments = "TRUE";
$eventNoti->EnvelopeEvents = array($envEvent); // <------------ Add multiple EnvelopeEvent's
$envInfo->EventNotification = $eventNoti;
This link is where the above code is referenced from, along with further discussion that might help.
Another option is to use the DocuSign Connect module to push events to your external listener. The main difference between DocuSign Connect and the eventNotification is that eventNotification is per envelope, Connect is account wide and or user-wide.

Categories