azure function service bus output message properties - javascript

I'm trying to set metadata for service bus messages in a JavaScript Azure Function using the service bus binding output. Unfortunately, it appears that the binding only supports the body.
Looking at the docs, I see that you can access this information in service bus triggers via context.bindingData but I don't see any corresponding interface for service bus output.
Is there some way to send a full brokered message and set the message properties (ContentType) and message custom properties?

#l--''''''---------'''''''''''' You need to access the Microsoft.Azure.ServiceBus.Message class. Let's say you have some json called messageBody
and you have some list of properties that you want to add to the message. You can achive it like the below example.
Make sure you add using Microsoft.Azure.ServiceBus;
var myCustomProperties = new List<Dictionary<string,string>>();
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
foreach (var userProperty in myCustomProperties)
{
message.UserProperties.Add(userProperty.Key, userProperty.Value);
}

There is an open issue for this at https://github.com/Azure/Azure-Functions/issues/454
Some customers seemed to have found a workaround. Perhaps you can try their approach mentioned here https://github.com/Azure/Azure-Functions/issues/454#issuecomment-375154151

Related

Google Analytics Management API - 'Unknown name "": Root element must be a message' when I try to update Google Analytics view

I have been struggling with this for a bit, but I am trying to find a programmatic way to update the Google Analytics views and change the name for all of the views. As a test, I wanted to start small and focus on only one views. Using App Script Console, I wrote this code:
function update_views() {
var view_id = "11545xxxx";
var payload = {"Name":"Updated .ca View"}
var payload_str = payload.toString();
//Logger.log(payload_str)
var profile = Analytics.Management.Profiles.update("xxxxx","UA-xxxxx-49","11545xxxx",payload_str)
}
But when I try to execute the function, I get the following error
GoogleJsonResponseException: API call to analytics.management.profiles.update failed with error: Invalid JSON payload received. Unknown name "": Root element must be a message.
update_views # update_views.gs:7
I even tried sending the data as an object and not a string, but the same end result.
Does anyone know what can be done?
This is seems quite confusing to me from api documentation.
Try to add the payload first something like
var profile = Analytics.Management.Profiles.update(payload_str,"xxxxx","UA-xxxxx-49","11545xxxx")}
This should fix the error you are getting.

Getting deviceId from Azure IotHub / EventHub

I am trying to read device Id from Event Hub (on the back of IoTHub) but my syntax in JS seems wrong.
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);
var deviceId = IoTHubMessages.SystemProperties["iothub-connection-device-id"];
The function returns an error: Exception: TypeError: Cannot read property 'iothub-connection-device-id' of undefined
I'm not entirely sure if "iothub-connection-device-id" is the correct name of the attribute on Event Hub but the problem seems to be with SystemProperties.
Appreciate any help.
iothub-connection-device-id is the right key to use, you just have to use it on the right property bag. An unrelated GitHub issue https://github.com/Azure/azure-sdk-for-js/issues/7801 shows how this key is indeed available on each message.
Depending on the cardinality in your functions.json file, IotHubMessages will either be an array of messages or a single message. See IOTHubMessage.forEach is not a function? for more details.
If it is an array of messages, accessing SystemProperties directly on it will not work. You will need to loop through to access each message separately.
Do you see systemProperties on the individual messages? If yes, then message.systemProperties["iothub-connection-device-id"] should work.
You should be reading messages this way. Read this for more about regarding the topic - https://learn.microsoft.com/en-us/samples/azure-samples/functions-js-iot-hub-processing/processing-data-from-iot-hub-with-azure-functions/
IoTHubMessages.forEach(message => {
context.log(`Processed message: ${message}`);
count++;
totalTemperature += message.temperature;
totalHumidity += message.humidity;
deviceId = message.deviceId;
});
First, use the JSON.stringify to print you the payload received. Secondly i believe you should be able to access your device id by doing the following: message.annotations["iothub-connection-device-id"]. For more info, please reference to the Quickstart examples you have available in the Microsoft's Github repos. Navigate to the iot-hub\Quickstarts\read-d2c-messages folder and you should find the example of processing the message payload and printing the output.
I landed on this question when looking for the deviceId when a deviceTwinChange happens on on Azure Iot hub and the message it routed through event hub to my Azure function. In IotHubMessage I wave only getting reported or desired information. I was looking for the deviceId so i knew what device it came from
properties: {
reported: {
//everything in my reported section
}
}
But i found out this:
module.exports = function (context, IoTHubMessages) {
The device Id is in the context variable. I just did not on Azure Iot hub using an Azure function.
var deviceId = context.bindingData.systemProperties["iothub-connection-device-id"];
A bit annoying that the metadata is kept in context and there is no documentation about this.
Extra points: There is no application properties in the context. Does anyone know how to get application properties in the information send to the azure function? This is for when you enrich the data from azure iot hub
This is the information in azure iot hub when doing the routing. Just havent seen the information in my azure function come through.
Add up to 10 message enrichments per IoT Hub. These are added as application properties to messages sent to chosen endpoint(s).

Mirth channelMap in source JavaScript

In my source connector, I'm using javascript for my database work due to my requirements and parameters.
The end result is storing the data.
ifxResults = ifxConn.executeCachedQuery(ifxQuery); //var is declared
I need to use these results in the destination transformer.
I have tried channelMap.put("results", ifxResults);.
I get the following error ReferenceError: "channelMap" is not defined.
I have also tried to use return ifxResults but I'm not sure how to access this in the destination transformer.
Do you want to send each row as a separate message through your channel? If so, sounds like you want to use the Database Reader in JavaScript mode. Just return that ResultSet (it's really a CachedRowSet if you use executeCachedQuery like that) and the channel will handle the rest, dispatching an XML representation of each row as discrete messages.
If you want to send all rows in the result set aggregated into a single message, that will be possible with the Database Reader very soon: MIRTH-2337
Mirth Connect 3.5 will be released next week so you can take advantage of it then. But if you can't wait or don't want to upgrade then you can still do this with a JavaScript Reader:
var processor = new org.apache.commons.dbutils.BasicRowProcessor();
var results = new com.mirth.connect.donkey.util.DonkeyElement('<results/>');
while (ifxResults.next()) {
var result = results.addChildElement('result');
for (var entries = processor.toMap(ifxResults).entrySet().iterator(); entries.hasNext();) {
var entry = entries.next();
result.addChildElement(entry.getKey(), java.lang.String.valueOf(entry.getValue()));
}
}
return results.toXml();
I know this question is kind of old, but here's an answer just for the record.
For this answer, I'm assuming that you are using a Source connector type of JavaScript Reader, and that you're trying to use channelMap in the JavaScript Reader Settings editing pane.
The problem is that the channelMap variable isn't available in this part of the channel. It's only available in filters and transformers.
It's possible that what you want can be accomplished by using the globalChannelMap variable, e.g.
globalChannelMap.put("results", ifxResults);
I usually need to do this when I'm processing one record at a time and need to pass some setting to the destination channel. If you do it like I've done in the past, then you would first create a globalChannelMap key/value in the source channel's transformer:
globalchannelMap.put("ProcID","TestValue");
Then go to the Destinations tab and select your destination channel to make sure you're sending it to the destination (I've never tried this for channels with multiple destinations, so I'm not sure if anything different needs to be done).
Destination tab of source channel
Notice that ProcID is now listed in the Destination Mappings box. Click the New button next to the Map Variable box and you'll see Variable 1 appear. Double click on that and put in your mapping key, which in this case is ProcID.
Now go to your destination channel's source transformer. There you would enter the following code:
var SentValue = sourceMap.get("ProcID");
Now SentValue in your destination transformer has whatever was in ProcID when your source channel relinquished control.

How to programmatically subscribe a user to a google calendar using javascript?

Hey i'm able to authenticate and authorize a user with the javascript API for google calendar. what i want to do next is subscribe that user to a public calendar. I was thinking i could just call the google.gdata.calendar.CalendarEntry constructor with the id of the calendar but that didn't work
var entry = google.gdata.calendar.CalendarEntry("idOfCalendar");
i also tried creating an instance of a entry id with google.gdata.atom.Id("idOfCalendar"); and adding that to the CalendarEntry constructor. Using the set methods didn't work either.
I use the InsertEntry method to add the entry but i get the following error
Error: Valid calendar id must be supplied for adding calendar to favorites list in allcalendars projection.
I can access the events of this calendar using google.gdata.calendar.CalendarEventQuery()
The google api for javascript doesn't give a lot of examples anyone know the answer to my problem or a good resource for working with the google calendar api? do you think i should be using php or jason instead?
** Edit
I found an example of what I want in the Java Api link so i tried
function addSubscriptionToCalendar() {
var feedUri = "http://www.google.com/calendar/feeds/default/allcalendars/full";
var calendarEntry = new google.gdata.calendar.CalendarEntry();
calendarEntry.setId("nhl_21_%54oronto+%4daple+%4ceafs#sports#group.v.calendar.google.com");
calendarService.insertEntry(feedUri, calendarEntry, function(){alert("calendar added")}, handleError);
}
but i got the same error
You should be using owncalendars feed to modify/add calendar entry for the authenticated user not allcalendars.
Sounds like your calendar id is invalid. The id from your second example appears to be URL encoded. Try using the decoded value:
nhl_21_Toronto Maple Leafs#sports#group.v.calendar.google.com
The gdata API for calendars has been switched off; the new way to do this is via: https://developers.google.com/google-apps/calendar/v3/reference/calendarList/insert

How do I catch an invalid API key for google maps

I have this code:
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=babab" type='text/javascript'></script>
If the key is invalid then it pops up an alert, but I want to perform some action in this case. I'm not sure how to hook into it though. Any ideas?
Google does not offer an external method of checking the Google Maps API key. Hence you cannot query some service with e.g. "Is this code valid abcde1234" and get a TRUE/FALSE response.
There is a discussion on how the Maps API key is generated. But I suggest you look at a post from Mike Williams about the GValidateKey function. This is the function actually doing the magic validation - what it exactly does, like creating a hash from your Google account / domain - we don't know.
I see two solutions for your problem of checking whether the API key provided is correct:
Overwrite the incoming alert with some custom code (check for the content of the alert, or check if an alert occurs withing X seconds after page load)
Somehow get the GValidateKey function to validate your key beforehand. Maybe you can call it before referencing the API Javascript? Sounds kind of hackish to me...
The problem you will likely have is that you don't know what Google actually checks. The referrer, the referring site, the host - many possibilities (it is not the IP address of the server, but the name plus some additional information).
I just ran across the need to perform an action if an invalid API key was used. Google's documentation states:
If you want to programmatically detect an authentication failure (for example to automatically send an beacon) you can prepare a callback function. If the following global function is defined it will be called when the authentication fails.
This was all I needed to do:
function gm_authFailure() { // Perform action(s) }
For modern browsers (IE9+ and others) you may use DOMNodeRemoved event. You just need to add event handler to the element that you pass to the map constructor:
var map = new google.maps.Map(element, myOptions);
element.addEventListener("DOMNodeRemoved", function(e){
if (e.target === element){
//your code here
element.removeEventListener("DOMNodeRemoved", mapWasRemovedHandler, true);
}
}, false);

Categories

Resources