Facebook Messenger Bot cannot retrieve location as attachment in IOS - c#

I have built a bot the needs to get user's location as longitude/latitude, it was working perfectly fine in both android and iOS, but right now I can get the location from messenger installed on android device, but cannot achieve that on a messenger installed on iOS device! any help?
Note: am using a c# class that receives the callback from facebook, not the Microsoft Bot Frameworks.
[ActionName("Receive")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReceivePost(BotRequest data)
{
Task.Factory.StartNew(() =>
{
foreach (var entry in data.entry)
{
foreach (var message in entry.messaging)
{
foreach (var item in message.message.attachments)
{
if (item.payload != null)
{
// here i can get the location
// if the location sent from android I can get attachment with long/lat
// if location sent from iOS I get attachment as null
var location = item.payload.coordinates;
}
}
}
}
});
return new HttpStatusCodeResult(HttpStatusCode.OK);}
BotRequest class:
using Newtonsoft.Json;
using System.Collections.Generic;
public class BotRequest
{
public string #object { get; set; }
public List<BotEntry> entry { get; set; }
}
public class BotEntry
{
public string id { get; set; }
public long time { get; set; }
public List<BotMessageReceivedRequest> messaging { get; set; }
}
public class FBUser
{
public string first_name { get; set; }
public string last_name { get; set; }
public string profile_pic { get; set; }
public string locale { get; set; }
public int timezone { get; set; }
public string gender { get; set; }
}
public class BotMessageReceivedRequest
{
public BotUser sender { get; set; }
public BotUser recipient { get; set; }
public string timestamp { get; set; }
public BotMessage message { get; set; }
public BotPostback postback { get; set; }
}
public class BotPostback
{
public string payload { get; set; }
}
public class BotMessageResponse
{
public BotUser recipient { get; set; }
public MessageResponse message { get; set; }
}
public class BotMessage
{
public string mid { get; set; }
public List<MessageAttachment> attachments { get; set; }
public long seq { get; set; }
public string text { get; set; }
public QuickReply quick_reply { get; set; }
}
public class BotUser
{
public string id { get; set; }
}
public class MessageResponse
{
public dynamic attachment { get; set; }
public List<QuickReply> quick_replies { get; set; }
public string text { get; set; }
}
public class QuickReply
{
public string content_type { get; set; }
public string title { get; set; }
public string payload { get; set; }
}
public class ResponseButtons
{
public string type { get; set; }
public string title { get; set; }
public string payload { get; set; }
public string url { get; set; }
public string webview_height_ratio { get; set; }
}
public class MessageAttachment
{
public string type { get; set; }
public MessageAttachmentPayLoad payload { get; set; }
}
public class MessageAttachmentPayLoad
{
public string url { get; set; }
public string template_type { get; set; }
public string top_element_style { get; set; }
public List<PayloadElements> elements { get; set; }
public List<ResponseButtons> buttons { get; set; }
public string recipient_name { get; set; }
public string order_number { get; set; }
public string currency { get; set; }
public string payment_method { get; set; }
public string order_url { get; set; }
public string timestamp { get; set; }
public Address address { get; set; }
public Summary summary { get; set; }
public coordinates coordinates { get; set; }
}
public class coordinates
{
public string lat { get; set; }
public string #long { get; set; }
}
public class PayloadElements
{
public string title { get; set; }
public string image_url { get; set; }
public string subtitle { get; set; }
public List<ResponseButtons> buttons { get; set; }
public string item_url { get; set; }
public int? quantity { get; set; }
public decimal? price { get; set; }
public string currency { get; set; }
}
public class Address
{
internal string street_2;
public string street_1 { get; set; }
public string city { get; set; }
public string postal_code { get; set; }
public string country { get; set; }
public string state { get; set; }
}
public class Summary
{
public decimal? subtotal { get; set; }
public decimal? shipping_cost { get; set; }
public decimal? total_tax { get; set; }
public decimal total_cost { get; set; }
}

Related

App is freezing when trying to deserialize Json string to object

I'm not sure what is causing this to happen. When debugging, I can step through each line, but when it hits the DeserializeObject line, the program doesn't do anything. I can no longer step over or do anything else with the program. No error messages are being thrown either.
[Command("stats")]
public async Task LookupMonsterStats(CommandContext ctx, string name)
{
string monstersUri = "https://www.dnd5eapi.co/api/monsters/";
var formatted = name.Replace(" ", "-");
string apiResponse = string.Empty;
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync(monstersUri + formatted))
{
apiResponse = await response.Content.ReadAsStringAsync();
var monster = JsonConvert.DeserializeObject<Monster>(apiResponse);
}
}
await ctx.Channel.SendMessageAsync(apiResponse).ConfigureAwait(false);
}
Edit: Here is what my Monster class looks like
public class Monster
{
[JsonProperty("index")]
public string Index { get; set; }
[JsonProperty("name")]
public string Name{ get; set; }
[JsonProperty("size")]
public string Size { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("subtype")]
public string Subtype { get; set; }
[JsonProperty("alignment")]
public string Alignment { get; set; }
[JsonProperty("armor_class")]
public int? ArmorClass { get; set; }
[JsonProperty("hit_points")]
public int? HitPoints { get; set; }
[JsonProperty("forms")]
public List<string> Forms { get; set; }
[JsonProperty("speed")]
public Object Speed { get; set; }
[JsonProperty("strength")]
public int? Strength { get; set; }
[JsonProperty("dexterity")]
public int? Dexterity { get; set; }
[JsonProperty("constitution")]
public int? Constitution { get; set; }
[JsonProperty("intelligence")]
public int? Intelligence { get; set; }
[JsonProperty("wisdom")]
public int? Wisdom { get; set; }
[JsonProperty("charisma")]
public int? Charisma { get; set; }
[JsonProperty("proficiencies")]
public List<string> Proficiencies { get; set; }
[JsonProperty("damage_vulnerabilities")]
public List<string> DamageVulnerabilities { get; set; }
[JsonProperty("damage_resistances")]
public List<string> DamageResistances { get; set; }
[JsonProperty("damage_immunities")]
public List<string> DamageImmunities { get; set; }
[JsonProperty("condition_immunities")]
public List<string> ConditionImmunities { get; set; }
[JsonProperty("senses")]
public Object Senses { get; set; }
[JsonProperty("languages")]
public string Languages { get; set; }
[JsonProperty("challenge_rating")]
public decimal? ChallengeRating { get; set; }
[JsonProperty("special_abilities")]
public List<string> SpecialAbilities { get; set; }
[JsonProperty("actions")]
public List<string> Actions { get; set; }
[JsonProperty("legendary_actions")]
public List<string> LegendaryActions { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
}
As mentioned in a comment your class definition doesn't seem to match the JSON response, assuming you're using Visual Studio it's probably worth opening Debug / Windows / Exception Settings and make sure "Common Language Runtime Exceptions" is checked. Sometimes I've found stepping over in multi-threaded apps can produce unusual results after an exception.
When I pasted your code into LINQPad I got errors on several members you've defined as List<string> when the underlying type is more complex. What I normally do to avoid those errors is copy the raw json from a browser window to the clipboard and with a CS file open in Visual Studio do an Edit / Paste Special / Paste JSON as classes to automatically create the classes. After doing that I got the following that deserialized the data fine:
async Task Main()
{
await LookupMonsterStats("aboleth");
}
public async Task LookupMonsterStats(string name)
{
string monstersUri = "https://www.dnd5eapi.co/api/monsters/";
var formatted = name.Replace(" ", "-");
string apiResponse = string.Empty;
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync(monstersUri + formatted))
{
apiResponse = await response.Content.ReadAsStringAsync();
var monster = JsonConvert.DeserializeObject<Monster>(apiResponse);
monster.Dump();
}
}
}
public class Monster
{
public string index { get; set; }
public string name { get; set; }
public string size { get; set; }
public string type { get; set; }
public object subtype { get; set; }
public string alignment { get; set; }
public int armor_class { get; set; }
public int hit_points { get; set; }
public string hit_dice { get; set; }
public Speed speed { get; set; }
public int strength { get; set; }
public int dexterity { get; set; }
public int constitution { get; set; }
public int intelligence { get; set; }
public int wisdom { get; set; }
public int charisma { get; set; }
public Proficiency[] proficiencies { get; set; }
public object[] damage_vulnerabilities { get; set; }
public object[] damage_resistances { get; set; }
public object[] damage_immunities { get; set; }
public object[] condition_immunities { get; set; }
public Senses senses { get; set; }
public string languages { get; set; }
public int challenge_rating { get; set; }
public int xp { get; set; }
public Special_Abilities[] special_abilities { get; set; }
public Action[] actions { get; set; }
public Legendary_Actions[] legendary_actions { get; set; }
public string url { get; set; }
}
public class Speed
{
public string walk { get; set; }
public string swim { get; set; }
}
public class Senses
{
public string darkvision { get; set; }
public int passive_perception { get; set; }
}
public class Proficiency
{
public Proficiency1 proficiency { get; set; }
public int value { get; set; }
}
public class Proficiency1
{
public string index { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Special_Abilities
{
public string name { get; set; }
public string desc { get; set; }
public Dc dc { get; set; }
}
public class Dc
{
public Dc_Type dc_type { get; set; }
public int dc_value { get; set; }
public string success_type { get; set; }
}
public class Dc_Type
{
public string index { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Action
{
public string name { get; set; }
public string desc { get; set; }
public Options options { get; set; }
public Damage[] damage { get; set; }
public int attack_bonus { get; set; }
public Dc1 dc { get; set; }
public Usage usage { get; set; }
}
public class Options
{
public int choose { get; set; }
public From[][] from { get; set; }
}
public class From
{
public string name { get; set; }
public int count { get; set; }
public string type { get; set; }
}
public class Dc1
{
public Dc_Type1 dc_type { get; set; }
public int dc_value { get; set; }
public string success_type { get; set; }
}
public class Dc_Type1
{
public string index { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Usage
{
public string type { get; set; }
public int times { get; set; }
}
public class Damage
{
public Damage_Type damage_type { get; set; }
public string damage_dice { get; set; }
}
public class Damage_Type
{
public string index { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Legendary_Actions
{
public string name { get; set; }
public string desc { get; set; }
public int attack_bonus { get; set; }
public Damage1[] damage { get; set; }
}
public class Damage1
{
public Damage_Type1 damage_type { get; set; }
public string damage_dice { get; set; }
}
public class Damage_Type1
{
public string index { get; set; }
public string name { get; set; }
public string url { get; set; }
}

C# object that represents TFS service hook events

I'm looking for a C# object that matches the sample payload of the build.complete event found at https://learn.microsoft.com/en-us/azure/devops/service-hooks/events?view=vsts#build.complete. The reference page recommends a nuget package but I can't find a "BuildCompleteEvent" in it.
I am looking for this object because I have a TFS service hook outputting to an Azure Storage Queue, and when I read that message off the queue in an Azure Function I want to be able to parse the message string as a useful C# object.
For "Build completed" event:
public class Message
{
public string text { get; set; }
public string html { get; set; }
public string markdown { get; set; }
}
public class DetailedMessage
{
public string text { get; set; }
public string html { get; set; }
public string markdown { get; set; }
}
public class Drop
{
public string location { get; set; }
public string type { get; set; }
public string url { get; set; }
public string downloadUrl { get; set; }
}
public class Log
{
public string type { get; set; }
public string url { get; set; }
public string downloadUrl { get; set; }
}
public class LastChangedBy
{
public string id { get; set; }
public string displayName { get; set; }
public string uniqueName { get; set; }
public string url { get; set; }
public string imageUrl { get; set; }
}
public class Definition
{
public int batchSize { get; set; }
public string triggerType { get; set; }
public string definitionType { get; set; }
public int id { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class Queue
{
public string queueType { get; set; }
public int id { get; set; }
public string name { get; set; }
public string url { get; set; }
}
public class RequestedFor
{
public string id { get; set; }
public string displayName { get; set; }
public string uniqueName { get; set; }
public string url { get; set; }
public string imageUrl { get; set; }
}
public class Request
{
public int id { get; set; }
public string url { get; set; }
public RequestedFor requestedFor { get; set; }
}
public class Resource
{
public string uri { get; set; }
public int id { get; set; }
public string buildNumber { get; set; }
public string url { get; set; }
public DateTime startTime { get; set; }
public DateTime finishTime { get; set; }
public string reason { get; set; }
public string status { get; set; }
public string dropLocation { get; set; }
public Drop drop { get; set; }
public Log log { get; set; }
public string sourceGetVersion { get; set; }
public LastChangedBy lastChangedBy { get; set; }
public bool retainIndefinitely { get; set; }
public bool hasDiagnostics { get; set; }
public Definition definition { get; set; }
public Queue queue { get; set; }
public List<Request> requests { get; set; }
}
public class Collection
{
public string id { get; set; }
}
public class Account
{
public string id { get; set; }
}
public class Project
{
public string id { get; set; }
}
public class ResourceContainers
{
public Collection collection { get; set; }
public Account account { get; set; }
public Project project { get; set; }
}
public class RootObject
{
public string id { get; set; }
public string eventType { get; set; }
public string publisherId { get; set; }
public string scope { get; set; }
public Message message { get; set; }
public DetailedMessage detailedMessage { get; set; }
public Resource resource { get; set; }
public string resourceVersion { get; set; }
public ResourceContainers resourceContainers { get; set; }
public DateTime createdDate { get; set; }
}

C# Get Json Response into an Array

Through webrequest I am getting response text which is in Json I use Newtonsoft.Json to parse. I created classes with the help of examples from stackoverflow website but couldn't figure out how to loop whole response into an array or datatable/dataview.
JsonSerializer serializer = new JsonSerializer();
public class Link
{
public string rel { get; set; }
public string href { get; set; }
}
public class Naeringskode1
{
public string kode { get; set; }
public string beskrivelse { get; set; }
}
public class Postadresse
{
public string adresse { get; set; }
public string postnummer { get; set; }
public string poststed { get; set; }
public string kommunenummer { get; set; }
public string kommune { get; set; }
public string landkode { get; set; }
public string land { get; set; }
}
public class Beliggenhetsadresse
{
public string adresse { get; set; }
public string postnummer { get; set; }
public string poststed { get; set; }
public string kommunenummer { get; set; }
public string kommune { get; set; }
public string landkode { get; set; }
public string land { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Naeringskode2
{
public string kode { get; set; }
public string beskrivelse { get; set; }
}
public class Datum
{
public int organisasjonsnummer { get; set; }
public string navn { get; set; }
public string organisasjonsform { get; set; }
public string registreringsdatoEnhetsregisteret { get; set; }
public string registrertIMvaregisteret { get; set; }
public int antallAnsatte { get; set; }
public Naeringskode1 naeringskode1 { get; set; }
public Postadresse postadresse { get; set; }
public Beliggenhetsadresse beliggenhetsadresse { get; set; }
public int overordnetEnhet { get; set; }
public List<Link2> links { get; set; }
public string hjemmeside { get; set; }
public Naeringskode2 naeringskode2 { get; set; }
}
public class Page
{
public int size { get; set; }
public int page { get; set; }
}
public class RootObject
{
public List<Link> links { get; set; }
public List<Datum> data { get; set; }
public Page page { get; set; }
}

How to parse Json from the RootObject?

I am trying to parse JSON response so I created some classes.
Actually I want Leg and Flight class element value. I am trying to get those element value from RootObject but I don't know how to do this. I googled but I am little bit confuse.
I paste my JSON respose , Classes !!
JSON Response :
http://pastebin.com/fjjLxkd2
Classes :
public class Detail
{
}
public class Airport
{
public string kind { get; set; }
public string code { get; set; }
public string city { get; set; }
public string name { get; set; }
}
public class City
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Aircraft
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Tax
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Carrier
{
public string kind { get; set; }
public string code { get; set; }
public string name { get; set; }
}
public class Data
{
public string kind { get; set; }
public List<Airport> airport { get; set; }
public List<City> city { get; set; }
public List<Aircraft> aircraft { get; set; }
public List<Tax> tax { get; set; }
public List<Carrier> carrier { get; set; }
}
public class Flight
{
public string carrier { get; set; }
public string number { get; set; }
}
public class Leg
{
public string kind { get; set; }
public string id { get; set; }
public string aircraft { get; set; }
public string arrivalTime { get; set; }
public string departureTime { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string originTerminal { get; set; }
public int duration { get; set; }
public int onTimePerformance { get; set; }
public int mileage { get; set; }
public string meal { get; set; }
public bool secure { get; set; }
public string destinationTerminal { get; set; }
public string operatingDisclosure { get; set; }
}
public class Segment
{
public string kind { get; set; }
public int duration { get; set; }
public Flight flight { get; set; }
public string id { get; set; }
public string cabin { get; set; }
public string bookingCode { get; set; }
public int bookingCodeCount { get; set; }
public string marriedSegmentGroup { get; set; }
public List<Leg> leg { get; set; }
public int connectionDuration { get; set; }
}
public class Slouse
{
public string kind { get; set; }
public int duration { get; set; }
public List<Segment> segment { get; set; }
}
public class Fare
{
public string kind { get; set; }
public string id { get; set; }
public string carrier { get; set; }
public string origin { get; set; }
public string destination { get; set; }
public string basisCode { get; set; }
}
public class BagDescriptor
{
public string kind { get; set; }
public string commercialName { get; set; }
public int count { get; set; }
public string subcode { get; set; }
public List<string> description { get; set; }
}
public class FreeBaggageOption
{
public string kind { get; set; }
public List<BagDescriptor> bagDescriptor { get; set; }
public int pieces { get; set; }
}
public class SegmentPricing
{
public string kind { get; set; }
public string fareId { get; set; }
public string segmentId { get; set; }
public List<FreeBaggageOption> freeBaggageOption { get; set; }
}
public class Passengers
{
public string kind { get; set; }
public int adultCount { get; set; }
}
public class Tax2
{
public string kind { get; set; }
public string id { get; set; }
public string chargeType { get; set; }
public string code { get; set; }
public string country { get; set; }
public string salePrice { get; set; }
}
public class Pricing
{
public string kind { get; set; }
public List<Fare> fare { get; set; }
public List<SegmentPricing> segmentPricing { get; set; }
public string baseFareTotal { get; set; }
public string saleFareTotal { get; set; }
public string saleTaxTotal { get; set; }
public string saleTotal { get; set; }
public Passengers passengers { get; set; }
public List<Tax2> tax { get; set; }
public string fareCalculation { get; set; }
public string latestTicketingTime { get; set; }
public string ptc { get; set; }
}
public class TripOption
{
public string kind { get; set; }
public string saleTotal { get; set; }
public string id { get; set; }
public List<Slouse> slice { get; set; }
public List<Pricing> pricing { get; set; }
}
public class Trips
{
public string kind { get; set; }
public string requestId { get; set; }
public Data data { get; set; }
public List<TripOption> tripOption { get; set; }
}
public class RootObject
{
public string kind { get; set; }
public Trips trips { get; set; }
}
Code :
var obj0 = JsonConvert.DeserializeObject<RootObject>(responsedata);
Here I got only Trip class element . I want the Leg and Flight class element.
If you want to find information for a specific leg, you need to use Linq to traverse the tree. For example, if you just know the leg id, you could do something like this:
var allLegs = obj0.trips.tripOption.SelectMany(to => to.slice.SelectMany(sl => sl.segment.Select(sg => sg.leg)));
var leg = allLegs.FirstOrDefault(l => l.id == "yourId");
The query for retrieving a flight would be similar. You could also filter by tripOption id to get a specific tripOption and then retrieve flights or legs that are associated with it.

Windows 8 store , Json Deserialize

im trying to deserialize a Json that i get from a webservice Like so:
Uri urlJson = new Uri("http://optimizingconcepts.com/staging/cartuxa/system/json_request.php?lang=pt");
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(urlJson);
var jsonString = await response.Content.ReadAsStringAsync();
the Json i get is this:http://paste2.org/MpYJd8kk
I then do this to get the JsonObject:
JsonObject root = Windows.Data.Json.JsonValue.Parse(jsonString).GetObject();
What i want to know, is the correct way of saving each object from json to a list
so the objects from "home" would be in a List, the objects from "artigos" would go to a List and so on.
Using Json.Net, it can be done as follows
Uri urlJson = new Uri("http://optimizingconcepts.com/staging/cartuxa/system/json_request.php?lang=pt");
var client = new HttpClient();
var json = await client.GetStringAsync(urlJson);
var obj = JsonConvert.DeserializeObject<Cartuxa.RootObject>(json);
I used http://json2csharp.com/ to generate below classes.
public class Cartuxa
{
public class Home
{
public object id { get; set; }
public string menu { get; set; }
public string title { get; set; }
public string image { get; set; }
public string url { get; set; }
}
public class Artigo
{
public string menu { get; set; }
public string submenu { get; set; }
public string title { get; set; }
public string subtitle { get; set; }
public string description { get; set; }
public List<object> media { get; set; }
}
public class Year
{
public string year { get; set; }
public string title { get; set; }
public string description { get; set; }
public string file { get; set; }
public string url { get; set; }
}
public class Product
{
public string id { get; set; }
public string image { get; set; }
public string url { get; set; }
public List<Year> year { get; set; }
}
public class Vinho
{
public string id { get; set; }
public string menu { get; set; }
public string submenu { get; set; }
public string title { get; set; }
public List<Product> product { get; set; }
}
public class Year2
{
public string year { get; set; }
public string title { get; set; }
public string description { get; set; }
public string file { get; set; }
public string url { get; set; }
}
public class Product2
{
public string id { get; set; }
public string image { get; set; }
public string url { get; set; }
public List<Year2> year { get; set; }
}
public class Azeite
{
public string id { get; set; }
public string menu { get; set; }
public string submenu { get; set; }
public string title { get; set; }
public List<Product2> product { get; set; }
}
public class Agente
{
public string id { get; set; }
public string continent_id { get; set; }
public string country_id { get; set; }
public string title { get; set; }
public string description { get; set; }
}
public class Continente
{
public string id { get; set; }
public string title { get; set; }
}
public class Pais
{
public string id { get; set; }
public string continent_id { get; set; }
public string title { get; set; }
}
public class Contacto
{
public string id { get; set; }
public string menu_id { get; set; }
public string submenu_id { get; set; }
public string title { get; set; }
public string address { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public string longitude { get; set; }
public string latitude { get; set; }
}
public class Premio
{
public string image { get; set; }
public string url { get; set; }
}
public class Noticia
{
public string id { get; set; }
public string menu { get; set; }
public string date_created { get; set; }
public string title { get; set; }
public string subtitle { get; set; }
public string description { get; set; }
public List<object> media { get; set; }
}
public class RootObject
{
public List<Home> home { get; set; }
public List<Artigo> artigos { get; set; }
public List<Vinho> vinhos { get; set; }
public List<Azeite> azeites { get; set; }
public List<Agente> agentes { get; set; }
public List<Continente> continentes { get; set; }
public List<Pais> paises { get; set; }
public List<Contacto> contactos { get; set; }
public List<Premio> premios { get; set; }
public List<Noticia> noticias { get; set; }
}
}

Categories