dynamic_template_data doesn't work with sendgrid and azure function integration - azure

Trying to use transactional template with azure function sendgrid integration (javaScript) , I'm sending the following object (removed email address etc.):
const message =
{
"personalizations":[
{
"to":[
{
"email":"[MY_EMAIL]",
"name":"Rotem"
}
],
"dynamic_template_data":{
"rotem_test1":"wow"
}
}
],
"from":{
"email":"[FROM_EMAIL]",
"name":"name"
},
"reply_to":{
"email":"[REPLY_EMAIL]",
"name":"name"
},
"template_id":"[CORRECT_TEMPLATE_ID]",
"tracking_settings":{
"click_tracking":{
"enable":true
}
}
}
context.done(null,message);
also tried using context.done(null,JSON.stringify(message)) with the same result:
I get an email from the correct template but without any substitution.
when sending the exact same object using the https://api.sendgrid.com/v3/mail/send
API using postman everything works well.
would love to get help here as for what I'm doing wrong on my azure function.

You did everything correctly as you can receive email as expected.
Problem is caused by the SDK version. Property dynamic_template_data has just been added in latest 9.10.0 Sendgrid C# SDK, but the binding extension still uses old version, which has no idea what dynamic_template_data is.
For 2.x function(Check Function app settings on Azure portal, see Runtime version: 2.xxx (~2)), we can install new version SDK manually before the extension is updated.
If you develop locally
Go to function project directory, delete bin, obj folder.
Edit extensions.csproj under function project, add latest version Sendgrid <PackageReference Include="Sendgrid" Version="9.10.0" />.
In this directory open console(terminal,Powershell,etc), input func extensions install to restore packages.
Else on Azure portal
Stop the function app.
Access kudu console through Platform Features -> Advanced Tools(Kudu) -> Debug Console (CMD)
Navigate to D:\home\site\wwwroot
Delete the bin directory
Edit extensions.csproj, add <PackageReference Include="Sendgrid" Version="9.10.0" /> and Save the changes.
In console below, input dotnet build extensions.csproj -o bin --no-incremental --packages D:\home\.nuget
After you see Build succeeded, start the function app.
For 1.x function(Runtime ~1), I am afraid we have to manually send email using with sendgrid node module, Sendgrid vesion in 1.x function is locked therefore can't be updated.

Related

Creating a main account twilio client on a subaccount node function

I'm developing a twilio function on a subaccount that needs to acces a twilio client on the main account. The function is being deployed through a Github action to the subaccount.
For this, on the subaccount function I have:
const mainClient = new twilio(context.MAIN_TWILIO_ACCOUNT_SID, context.MAIN_TWILIO_AUTH_TOKEN);
Then I need to access serverless.services but I'm getting serverless undefined therefore can't access services.
i.e.
return mainClient.serverless.services(...
I tested code locally and it worked but failed on deployment. Is it possible to access services on main account from a subaccount function??
Thanks.
For anyone experiencing a similar issue.
My problem was that deploying without being explicit about dependencies doesnt install the latests version of dependencies.
Deploy through github installed twilio node sdk version 3.29.0 but this version doesn includes serverless api yet. So I explicitly added latest twilio node sdk (3.75.0 at the moment) and that fixed the problem.
Thanks.

Swashbuckle for Azure Functions v3: failed to load API definition

I created a simple project where I added the latest version of the extension. I published my code on Github. I use Azure Functions v3. The dependencies are:
AzureExtensions.Swashbuckle 3.1.6
AzureFunctions.Extensions.Swashbuckle 1.4.4
Microsoft.NET.Sdk.Functions 3.0.3
I followed the steps on theSwashbuckle page. Locally is working. When I publish the function on Azure, I get this error:
{
"schemaValidationMessages": [
{
"level": "error",
"message": "Can't read from file https://azuks-test-q001.azurewebsites.net/api/swagger/json"
}
]
}
Before this, I faced another issue that I asked here. I can't understand what changed.
Update
For same reason, the code param can't be the same for json and UI. If you open your Swagger url with Get Function url and copy the url from UI function, it is working.
Currently only fork => https://github.com/vitalybibikov/azure-functions-extensions-swashbuckle
supports your version of Azure Functions, (which is v3)

Azure Text Moderator not found

API Name:
TextModerator
have Azure Subscription and I created the cognitive service, and from a c# console, I'm trying to test the TextModerator project.
When this line is called:
var screenResult =
client.TextModeration.ScreenText("text/plain", new MemoryStream(Encoding.UTF8.GetBytes(text)), "eng", true, true, null, true);
I got the following message:
Microsoft.CognitiveServices.ContentModerator.Models.APIErrorException
: 'Operation returned an invalid status code 'NotFound''
the endpoint is like this:
https://contentmoderator-****.cognitiveservices.azure.com/contentmoderator
Any Idea?
Saad
You can use the following base endpoint for your specific Region(ex:westus).
https://westus.api.cognitive.microsoft.com.
Install the client library:
Within the application directory, install the Content Moderator client library for .NET with the following command:
dotnet add package Microsoft.Azure.CognitiveServices.ContentModerator --version 2.0.0
If you're using the Visual Studio IDE, the client library is available as a downloadable NuGet package.
Please follow the below document for Moderate Text.
https://learn.microsoft.com/en-us/azure/cognitive-services/content-moderator/dotnet-sdk-quickstart#setting-up
Please follow the API Reference for more information.

React native azure notification hub

I am using this library to implement azure notification hub in react native.
I am able to get the registration ID successfully. But no notification is received.
I am sending test notification from azure portal and it is getting delivered successfully.
I am followed the integration tutorial from here and here.
In my manifest file i am getting this error in red. Could be the reason.
If yes, How do i resolve this ?
I followed the same steps and had the same issue with com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver being unreferenced. I was able to resolve by adding
dependencies {
...
implementation 'com.microsoft.azure:notification-hubs-android-sdk:0.6#aar'
implementation 'com.microsoft.azure:azure-notifications-handler:1.0.1#aar'
}
repositories {
maven {
url "https://dl.bintray.com/microsoftazuremobile/SDK"
}
}
to my app build.gradle as outlined here
It may or may not make a difference but also make sure you have the x86 Atom image installed to the project

Azure function: Could not load file or assembly Microsoft.IdentityModel.Tokens, Version=5.2.1.0

Im writing an azure function to generate a JWT token and return it to the client. The code is tested locally in a console app and all seems to work fine. This is the package reference included in the working console app, and in my functions app:
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" />
When running the function host locally with func host start and executing the code it results in the error:
Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."
I don't understand why this is happening, the dll is laying in the output folder along with my application dll. The only other thing I can think of is that the function host has its own set of packages that it sources from and this one is not available yet, having been only released 12 days ago.
I'm not sure. Any help on why this is happening or how to get around it?
Details:
Azure Functions Core Tools (2.0.1-beta.22)
Function Runtime Version: 2.0.11415.0
I got this issue and it seems to be related to some kind of bug in the Azure function SDK. the fix was to add:
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
to your csproj file. As documented here
I had installed this package Microsoft.AspNetCore.Authentication.JwtBearer
And for me, the issue was resolved.
You can uninstall System.IdentityModel.Tokens.Jwt
Because the Microsoft package depends on the system package, so it gets installed automatically.
I was able to solve this exact issue by using an older version of the nuget package. My starting point was that I had copied a class file from an old project to a new one. The class file referenced JwtSecurityToken. This did not compile in the new project, so I added Security.IdentityModel.Tokens.Jwt from nuget package manager. I just added latest version. This worked fine locally, but just like you, it failed when published to azure. I then looked at the old project and noticed that it was using 5.1.4 of that Security.IdentityModel.Tokens.Jwt. So, I downgraded to that version and it now works when published.
fwiw: this is the v2 preview runtime version at the time I did this.
https://<mysite>.azurewebsites.net/admin/host/status?code=<myadminkey>
{
"id": "<mysite>",
"state": "Running",
"version": "2.0.11587.0",
"versionDetails": "2.0.11587.0-beta1 Commit hash: 1e9e7a8dc8a68a3eff63ee8604926a8d3d1902d6"
}
tl;dr
None of the above worked for me and this would randomly happen from time to time until today it happened all the time. The only reason I could see was that Microsoft.IdentityModel.Tokens was not directly referenced in the executing project, but was on a referenced project. The library was sitting in the bin folder, it just wouldn't load.
Reference
Taking a clue from this solution to another problem I was able to resolve it like so:
Solution
Create a static constructor in the app's entry point class
static MainClass()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
Add the handler
private static System.Reflection.Assembly? CurrentDomain_AssemblyResolve(object? sender, ResolveEventArgs args)
{
var domain = sender as AppDomain;
var assemblies = domain.GetAssemblies();
foreach(var assembly in assemblies)
{
if (assembly.FullName.IsEqualTo(args.Name))
{
return assembly;
}
}
var folder = AppDomain.CurrentDomain.BaseDirectory;
var name = args.GetLibraryName().Name.Split(Symbols.Comma).FirstOrDefault();
var library = $"{name}.dll";
var file = Path.Combine(folder, library);
if (File.Exists(file))
{
return Assembly.LoadFrom(file);
}
return null;
}

Resources