Getting IotHub DeviceId with azure functions - c#

I have setup routing to a Service Bus Queue using Azure IoT Hub. I followed this tutorial to get it working: https://azure.microsoft.com/en-gb/blog/how-to-use-azure-functions-with-iot-hub-message-routing/.
In my function I am trying to get the DeviceId so that I can verify the device that the message came from. I have a string parameter called message which receives The message I have sent. However, I need to get the DeviceId from the message. I tried to change the type to EventData but I got a serialisation error because of the JSON message type I think.
I will update this post with the actual code and error message tomorrow but wondered if anyone had come across this issue?

Since you get it from Service Bus Queue, you should be using BrokeredMessage class instead of EvenData.
You should be able to extract Device Id from it.

Related

What and where is the DefaultFullSharedAccessSignature in my Azure Notification Hub

I am following the tutorial on here by microsoft on setting up a notification hub for an iOS app. The "Test Send" section of Notification Hub succeeds in sending the notification, and I actually receive it on my registered device.
However, when I invoke an notification from my C# backend, nothing happens. I even get HttpStatusCode.OK after invoking SendAppleNativeNotificationAsync.
So the Azure Hub setup in Azure portal seems to be fine since the test send works, but my backend is probably wrong somewhere.
On the code below:
private Notifications() {
Hub = NotificationHubClient.CreateClientFromConnectionString(
"<your hub's DefaultFullSharedAccessSignature>",
"<hub name>");
}
What exactly is DefaultFullSharedAccessSignature, and which part of my connection string do I get that?
At the moment here is what I have:
Hub = NotificationHubClient.CreateClientFromConnectionString(
"Endpoint=sb://myappnotificationsns.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=somerandomcharacterhere27374+7384bvbher38434fve0=",
"myappnotifications"
);
nb: thats a fake connection string.
Is this correct or wrong?
Okay, so turns out the above setup is correct.
My issue instead was that the username passed during token registration was null. There is no exception or error from the SDK for some reason. Had to hunt furiously to find the issue.
Hope this helps someone someday.

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.

Azure Notification Hubs Register Device Error 404

This is the first time I'm using Azure Notification Hubs and I'm having some trouble getting it working properly with my application.
The part I'm stuck on (at the moment) is registering my device with the notification hub. I'm using the backend method to do the registration ... that is, I'm creating an Installation object and using the CreateOrUpdateInstallationAsync method to register the device via my Web API. I'm only testing it at this stage so I'm hitting my API endpoint with dummy data via Postman.
When I step through my code, I'm getting the following error when I execute CreateOrUpdateInstallationAsync ...
The remote server returned an error: (404) Not Found. Entity does not
exist.TrackingId:203cba37-007d-4dcb-ae25-ced33fa012aa_G1,TimeStamp:2/4/2018
10:24:02 PM
I've tested that I am connecting to the Notification Hub correctly by calling GetAllRegistrationsAsync. This returns an empty list (expected) and no error ... so I have my endpoints set up correctly. I'm wondering if there is a problem with my dummy data? For the installation Id, I've just created a random GUID (Guid.NewGuid). The Device ID and Push Notification Handle are random numbers and letters. And I'm testing this for the Android platform (NotificationPlatform.Gcm).
Has anyone seen this error before and know what it means? Am I able to just use random data for testing purposes (I'm only interested in registering devices at this stage) or do I need legitimate data (real device id's, etc)?
Thanks in advance.
The CreateOrUpdateInstallationAsync method would essentially invoke the REST API Create or Overwrite an Installation. When you register with a notification hub from your custom backend using the Installation, the core code would look like as follows:
NotificationHubClient hubclient = NotificationHubClient.CreateClientFromConnectionString(listenConnString, hubName);
await hubclient.CreateOrUpdateInstallationAsync(installation);
Note: You could install the Microsoft.Azure.NotificationHubs package for back end operations.
For a simpler way, I just created a console application and test this operation as follows:
Note: I just created a new Azure Notification Hub and did not set any notification settings. And I set a GUID as the InstallationId and a random string as the PushChannel, the rest operation could work as expected.
And I could retrieve the previous added registration as follows:
Has anyone seen this error before and know what it means? Am I able to just use random data for testing purposes (I'm only interested in registering devices at this stage) or do I need legitimate data (real device id's, etc)?
The operation could work on my side, I would recommend you debug your application and leverage fiddler to capture the network traces to narrow this issue. Moreover, you could follow Registration management for more details about registering devices with azure notification hubs.
Ok, it turns out that I had the wrong value for Hub Name when instantiating the NotificationHub object using NotificationHubClient.CreateClientFromConnectionString. I was using the namespace, instead of the hub name (visible on the Overview tab in the Azure Portal).

Is it possible to determine From number used when using copilot messaging service

When i send a text to a customer using a messaging service id. The from number is null in the returned MessageResource. Code below.
Is the only way to determine the From by making an additional call out using the message sid? I need the from used for analytics and so I can use the same number to contact an account in case the customer uses multiple contact numbers.
return await MessageResource.CreateAsync
(
to: new PhoneNumber(toNumber),
messagingServiceSid: messengerSid,
body: message
);
Twilio developer evangelist here.
When you use a messaging service to send messages from various numbers then you cannot know at the time of message creation what the number used will be. When you make the request to send the message, Twilio queues that message up to be delivered by the messaging service. If you inspect the status of the message object you receive from the API call you show in your question you will see it is "accepted". At this point the messaging service won't have decided which number to use.
So, you can either make a second request to the API using the message SID that is returned to find out the From number. Or, you could set a StatusCallback URL which would receive a request once the message was sent, including the From number.
Let me know if that helps at all.

Is there a way to make Azure WebJobs ServiceBus out parameter optional?

I am building a console application that will be run as a continuous Azure WebJob. I am using the Azure WebJobs SDK via the Nuget Package Microsoft.Azure.Jobs.ServiceBus v0.3.1-beta (prerelease). I have static method that triggers on an Azure ServiceBus queue. I do some processing and then want to have the option to send a response via the output parameter to another queue. The method signature looks like this:
public static void TriggerOnQueue(
[ServiceBusTrigger(QueueName)] BrokeredMessage receivedMessage,
[ServiceBus(QueueResponseName)] out BrokeredMessage responseMessage)
{
...
}
My initial thought was to set the responseMessage to null. However, when I do this an error appears in the console window. It doesn't stop execution (so it technically does what I want it to do), but I would rather not push something throwing errors to production. Is there any non-hackish way to set a value in the response message that will not throw an error, but will not submit the message to the response queue?
If not, is there another pattern I am missing that I could use? I would prefer to use the pipeline feature of the WebJobs SDK as opposed to creating the output queue manually. I could probably submit the requests that need a response on to a separate queue and have 2 separate triggers, but with the small amount I am having this do I would rather keep it simple and together.
Thoughts?
This pattern of specifying null for an out parameter works for Azure Queues but it throws an exception for Service Bus Queues. This looks like bug in the SDK. I will open a bug for us to fix it. Thank you for reporting this issue

Categories