I am unable to get word substitution to work consistently with sendgrid v3 api in c#. Sometimes the tags will be substituted, other times they will not. I am at a loss as to what causes this. Can anyone see any obvious errors in my code?
String apiKey = "KEY";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("info#example.com");
String subject = "Hello World from the SendGrid CSharp Library";
Email to = new Email("example#gmail.com");
Content content = new Content("text/html", " ");
to.Name = "Joe";
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "dfea45f3-d608-4860-9f38-c7d444qwrqwc1f";
Personalization subs = new Personalization();
subs.AddTo(to);
subs.AddSubstitution("*|url|*", "http://asdasdasd.com");
subs.AddSubstitution("*|username|*", "MrUsername");
mail.AddPersonalization(subs);
dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
Remove the Personalization and add the following
mail.Personalization[0].AddSubstitution("*|url|*", "http://asdasdasd.com");
mail.Personalization[0].AddSubstitution("*|username|*", "MrUsername");
or see code samples here
I have found the issue my side. I think yours is the same. When you look at mail object before sending you will find there are 2 personification items in the array. You do this subs.AddTo(to); then later on mail.AddPersonalization(subs); This creates 2 email to in the personalization array - my example of the wrong payload was:
{
"from": {
"email": "no-reply#chromasports.com"
},
"subject": "",
"personalizations":
[
{
"to": [{
"email": "email#gmail.com"
}]
},
{
"to": [{
"email": "email#gmail.com"
}],
"substitutions": {
":token": "http://alabala.com/auth/reset-password#token=or514rqHTeLjtjlN6WRppOu53yJJ64nSzcK86GF6Ite2BaZRa58YPMfTmM0wzQs4tMLbHy8YlpieDVBae1aD99TKnMh7wYNOE2nmu8gWePQoZiWhbFLomVBvApHA1fuxIxQ1elui2QXAmGPtwDdvVOgvAiSF3HQteuvFwP5kXUnXXEeddYLIUHqJCDrATiOsSgxvcpKmhXhrhx78ns49f4hakGlLMncNgBuMGmL3wCduY9f22hjCs9tbIPq5h5V"
}
}
],
"content": [{
"type": "text/html",
"value": "\u003chtml\u003e\u003cbody\u003eHTML content\u003c/body\u003e\u003c/html\u003e"
}],
"template_id": "unique id"
}
'
Check your payload and to fix it try mail.AddPersonalization[0] = subs; Hope this solves your issue
Related
With this code:
var button = Value.ForStruct(new Struct{
Fields={
["type"] = Value.ForString("postback"),
["title"] = Value.ForString("Call Representative"),
["payload"] = Value.ForString("+15105551234"),
}
});
var inPayload = Value.ForStruct(new Struct{
Fields ={
["buttons"] = Value.ForList(button),
["text"] = Value.ForString("try the postback"),
["template_type"] = Value.ForString("button"),
}
});
var attachment = Value.ForStruct(new Struct{
Fields ={
["payload"] = inPayload,
["type"] = Value.ForString("template"),
}
});
var msg = Value.ForStruct(new Struct{
Fields ={
["attachment"] = attachment,
});
Payload = new Struct{
Fields ={
["facebook"] = msg
}
I was able to create the following json:
"payload": {
"facebook": {"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postback",
"template_type": "button"
},
"type": "template"
}}
Now I need to create the following other format but I dont find how to do it:
"payload": {
"message": "Yes I did it"
"platform": "kommunicate",
"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postback",
"template_type": "button"
},
"type": "template"
}
I really dont find how to eliminate the first "facebook": { element and leave only:
{
"message": "Yes I did it",
"platform": "kommunicate",
"attachment":
And include message and platform at the same level. Here is the complete json I will like to generate:
"payload": {
"platform": "kommunicate",
"message": "Yes I did it",
"attachment": {
"payload": {
"buttons": [ {
"type": "postback",
"title": "Call Representative",
"payload": "+15105551234"
}],
"text": "try the postbackggggggg",
"template_type": "button"
},
"type": "template"
}
If you want to take an object and convert it to json I would recommend taking a look at Newtonsoft Json.Net library. They have plenty of examples that might help you. There is also protobuf.net library for serializing to protobuf instead of json.
Both libraries are used in similar ways, you create a class with appropriate properties and set the values you want. You will need multiple classes for nested types as in your example. Protobuf requires you to annotate the properties with attributes, while this is optional for json.net. You then send the object to the serialization library and get a string or binary data representing your object. This kind of object is often called a Data Transfer Object (DTO), since the only purpose it has is to aid in serialization or/and transfering the data to another system.
How to dynamically change my custom adaptive card's text property value from within my C# code?
Here is my C# code
public static Attachment CreateMySearchCardAttachment()
{
// combine path for cross platform support
string[] paths = { ".", "Resources", "MySearchCard.json" };
var MySearchCardJson = File.ReadAllText(Path.Combine(paths));
var adaptiveCardAttachment = new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(MySearchCardJson),
};
return adaptiveCardAttachment;
}
And my MySearchCard.json file goes below
{
"type": "AdaptiveCard",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "Image",
"horizontalAlignment": "Right",
"spacing": "None",
"url": "",
"size": "Medium",
"width": "2px",
"height": "2px"
},
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Knowledgebase Search"
},
{
"type": "Input.Text",
"id": "searchText",
"placeholder": "Type your search text and click Search"
}
],
"width": 2
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Search"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
I am able to get this adaptive card to display inside my chat bot.But not sure how to dynamically change the text labels or their values. We want to dynamically change some of the text labels before displaying, then after displaying,dynamically show or hide based on user response.At a later point we want to integrate chat BoT with MS Teams.But prior to that I need to show the same from my emulator
If you look at my json, there is a text property with "text": "Knowledgebase Search". My question is how to change this text value dynamically from within my C# code?
Dynamically Changing Before Being Displayed
There's a few different ways to do this. The first option is probably the best, but they should all work.
1. Use the AdaptiveCards Package
Note: This package is different from, and newer than, Microsoft.AdaptiveCards -- Don't use this one
Since you know the exact part of the card that you'd like to change, you can:
string[] paths = { ".", "AdaptiveCard.json" };
var cardJson = File.ReadAllText(Path.Combine(paths));
var card = AdaptiveCard.FromJson(cardJson).Card;
var columnSet = (card.Body[0] as AdaptiveColumnSet);
var column = (columnSet.Columns[0] as AdaptiveColumn);
var knowledgeBlock = (column.Items[1] as AdaptiveTextBlock);
knowledgeBlock.Text = "Whatever You Want";
var attachment = new Attachment()
{
Content = card,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
Result:
2. Use Data Binding (in Preview)
This is in preview and you still need to use the NuGet package from #1, but makes it easier to modify particular fields.
3. Edit the JSON with Newtonsoft.JSON
This is probably a little simpler, but less flexible. Something like this works and produces the same result as #1:
string[] paths = { ".", "AdaptiveCard.json" };
var cardJsonObject = JObject.Parse(File.ReadAllText(Path.Combine(paths)));
var knowledgeToken = cardJsonObject.SelectToken("body[0].columns[0].items[1]");
knowledgeToken["text"] = "Whatever You Want";
var attachment = new Attachment()
{
Content = cardJsonObject,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
return await stepContext.NextAsync();
Dynamically Changing After Being Displayed
Changing the card after being displayed is a little more difficult. You first have to change the card in the code, as done above. You then have to use UpdateActivityAsync(). Basically, you send an activity with the same id, but a new card and it overwrites the previous card completely.
Note: You can only use this in channels that support updating activities. It's usually pretty easy to tell, because even without bots, the channel either will or won't let you edit messages. It sounds like you want to use Teams, so this will work fine.
You can use my answer here for how to update card activities with Teams. Note that this one is in Node, but you can still do it in C# the same way.
You can also use this other StackOverflow Answer from one of the guys on the MS Teams team.
I am working on a Windows Phone 8.1 application involving location. I am receiving Json data from my API. My API returns data that looks like:
[{
"country": "India",
"city": "Mall Road, Gurgaon",
"area": "Haryana",
"PLZ": "122002",
"street": "",
"house_no": "",
"POI": "",
"type": "17",
"phone": "",
"lng": 77.08972334861755,
"lat": 28.47930118040612,
"formatted_address": "Mall Road, Gurgaon 122002, Haryana, India"
},
{
"country": "India",
"city": "Mall Road, Kanpur",
"area": "Uttar Pradesh",
"PLZ": "208004",
"street": "",
"house_no": "",
"POI": "",
"type": "17",
"phone": "",
"lng": 80.35783410072327,
"lat": 26.46026740300029,
"formatted_address": "Mall Road, Kanpur 208004, Uttar Pradesh, India"
},
{
"country": "India",
"city": "Mall Road Area, Amritsar",
"area": "Punjab",
"PLZ": "143001",
"street": "",
"house_no": "",
"POI": "",
"type": "17",
"phone": "",
"lng": 74.87286686897278,
"lat": 31.64115178002094,
"formatted_address": "Mall Road Area, Amritsar 143001, Punjab, India"
},
{
"country": "India",
"city": "Vasant Kunj (Mall Road Kishan Garh), New Delhi",
"area": "Delhi",
"PLZ": "110070",
"street": "",
"house_no": "",
"POI": "",
"type": "18",
"phone": "",
"lng": 77.1434211730957,
"lat": 28.51363217008815,
"formatted_address": "Vasant Kunj (Mall Road Kishan Garh), New Delhi 110070, Delhi, India"
}]
I am deserializing my Json data and putting it into a class named LocationData. When I run my code, it gives me an error:
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path
Where am I going wrong? Here is my code:
private async void GetAPIData()
{
string _serviceUrl = "https://api.myweblinkapiprovider/v2&q=" + UserRequestedLocation;
HttpClient client = new HttpClient();
HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));
if (responce.Content != null)
{
var respArray = JObject.Parse(await responce.Content.ReadAsStringAsync());
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.MissingMemberHandling = MissingMemberHandling.Ignore;
var rcvdData = JsonConvert.DeserializeObject<LocationData>(respArray.ToString(), settings);
UpdateMapData(rcvdData);
UpdateTextData(rcvdData);
}
}
I also tried to use a JArray. My code is as below:
private async void GetAPIData()
{
string _serviceUrl = "https://api.myweblinkprovider.com/v3?fun=geocode&lic_key=MyKey" + UserRequestedLocation;
HttpClient client = new HttpClient();
HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));
JArray arr = JArray.Parse(await responce.Content.ReadAsStringAsync());
foreach (JObject obj in arr.Children<JObject>())
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.NullValueHandling = NullValueHandling.Ignore;
settings.MissingMemberHandling = MissingMemberHandling.Ignore;
var rcvdData = JsonConvert.DeserializeObject<LocationData>(arr.ToString(), settings);
UpdateMapData(rcvdData);
UpdateTextData(rcvdData);
}
}
It also gives me an error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MMI_SpeechRecog.Model.LocationData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
The first part of your question is a duplicate of Why do I get a JsonReaderException with this code?, but the most relevant part from that (my) answer is this:
[A] JObject isn't the elementary base type of everything in JSON.net, but JToken is. So even though you could say,
object i = new int[0];
in C#, you can't say,
JObject i = JObject.Parse("[0, 0, 0]");
in JSON.net.
What you want is JArray.Parse, which will accept the array you're passing it (denoted by the opening [ in your API response). This is what the "StartArray" in the error message is telling you.
As for what happened when you used JArray, you're using arr instead of obj:
var rcvdData = JsonConvert.DeserializeObject<LocationData>(arr /* <-- Here */.ToString(), settings);
Swap that, and I believe it should work.
Although I'd be tempted to deserialize arr directly as an IEnumerable<LocationData>, which would save some code and effort of looping through the array. If you aren't going to use the parsed version separately, it's best to avoid it.
In this case that you know that you have all items in the first place on array you can parse the string to JArray and then parse the first item using JObject.Parse
var jsonArrayString = #"
[
{
""country"": ""India"",
""city"": ""Mall Road, Gurgaon"",
},
{
""country"": ""India"",
""city"": ""Mall Road, Kanpur"",
}
]";
JArray jsonArray = JArray.Parse(jsonArrayString);
dynamic data = JObject.Parse(jsonArray[0].ToString());
I ran into a very similar problem with my Xamarin Windows Phone 8.1 app. The reason JObject.Parse(json) would not work for me was because my Json had a beginning "[" and an ending "]". In order to make it work, I had to remove those two characters. From your example, it looks like you might have the same issue.
jsonResult = jsonResult.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });
I was then able to use the JObject.Parse(jsonResult) and everything worked.
The following worked for me to convert a list of objects to json.
using Newtonsoft.Json;
static void Main(string[] args)
{
List<eventResponse> o = new List<eventResponse>()
{
new eventResponse { acknowledge = "test" } ,
new eventResponse { acknowledge = "test 2" }
};
var json = JsonConvert.SerializeObject(o);
JArray jo = JArray.Parse(json);
Console.WriteLine(jo);
}
public class eventResponse
{
public string acknowledge { get; set; }
}
A delayed answer but if you have access to the API you can work on the javascript object to make it as JSon. Something like
var jsonAddresses = { "addresses":
[
{
"country": "India",
"city": "Mall Road, Gurgaon",
},
{
"country": "India",
"city": "Mall Road, Kanpur",
}
]};
Then in C#
JObject Addjson = JObject.Parse(model.YourAddressesSampleJSONStr);
I am trying to post a new item creation to a test store via C#, but I'm not sure how the syntax should read. Square Connect API requires at least one variation for new item creation, but I'm not sure how to add that to the JSON body. Here is what I have, but I'm not sure how to complete it.
var client = new RestSharp.RestClient();
var post = new RestRequest("https://connect.squareup.com/v1/me/items", Method.POST);
post.RequestFormat = DataFormat.Json;
post.AddHeader("Authorization", String.Format("Bearer {0}", testtoken));
post.AddBody(new { name = testname, variations = ???? });
This code works, but returns a response of an item must include at least one variation. I realize that, but do not know how to write it, or if it is even possible.
I am not opposed to going a different route.
Edited to add a sample request body from the Square documentation:
{
"name": "Milkshake",
"description": "It's better than yours",
"visibility": "PRIVATE",
"category_id": "36ac7016-3a4e-4934-81f1-9057ac613f2y",
"variations": [
{
"name": "Small",
"pricing_type": "FIXED_PRICING",
"price_money": {
"currency_code": "USD",
"amount": 400
},
"sku": "123"
}
]
}
Something like this should serialize to JSON in the correct format:
post.AddBody(new {
name = testname,
variations = new object[] {
new {
name = "Small",
pricing_type = "FIXED_PRICING",
price_money = new {
currency_code = "USD",
amount = 400
}
}
},
sku = "123"
});
I am planning to write a simple SugarCrm .Net client.
Could anyone give me a pointer to a .Net wrapper/library for the SugarCrm REST api?
//lasse
Realize this is old, but in case anyone stumbles across this looking for a .NET wrapper for newer version of the web service (SugarCRM > 6.7), here is a wrapper I am currently building https://github.com/dlively1/SugarSharp
There is a wrapper that uses SOAP called CandyWrapper, which is a bit old but could help you with a starting point on building your own.
http://developers.sugarcrm.com/wordpress/2011/08/10/web-services-in-your-own-language-part-6-candywrapper-for-net/
I also realize this is old, but for SugarCRM CE 6.x I created SugarRestSharp. The sample I give below is in json, but the request would be in C# models. This implements get_entry_list SugarCrm Rest API method.
Passing request to RestSharp:
dynamic data = new
{
session = sessionId,
module_name = moduleName,
query = queryString,
order_by = string.Empty,
offset = 0,
select_fields = selectFields,
link_name_to_fields_array = string.Empty,
max_results = maxCountResult,
deleted = 0,
favorites = false
};
var client = new RestClient(url);
var request = new RestRequest(string.Empty, Method.POST);
string jsonData = JsonConvert.SerializeObject(data);
request.AddParameter("method", "get_entry_list");
request.AddParameter("input_type", "json");
request.AddParameter("response_type", "json");
request.AddParameter("rest_data", jsonData);
var response = client.Execute(request);
Request in json
{
"session": "olgg6hf5sqi6hk9u3tgpafbn66",
"module_name": "Accounts",
"query": "",
"order_by": "",
"offset": 0,
"select_fields": [
"name",
"industry",
"website",
"shipping_address_city",
"id"
],
"link_name_to_fields_array": "",
"max_results": 10,
"deleted": 0,
"favorites": false
}
Response in json
{
"result_count": 10,
"total_count": "58",
"next_offset": 10,
"entry_list": [
{
"id": "1e0eec64-8cc6-58ff-57f1-58533731b145",
"module_name": "Accounts",
"name_value_list": {
"name": {
"name": "name",
"value": "New SugarRestSharp Acccount 1 10397"
},
"industry": {
"name": "industry",
"value": "Manufacturing"
},
"website": {
"name": "website",
"value": "www.sugarrestsharp1.com"
},
"shipping_address_city": {
"name": "shipping_address_city",
"value": "Los Angeles"
},
"id": {
"name": "id",
"value": "1e0eec64-8cc6-58ff-57f1-58533731b145"
}
}
},
......... (other 9 items truncated)
],
"relationship_list": [
]
}
For more implementation and wiki check SugarRestSharp: https://github.com/mattkol/SugarRestSharp