예제 #1
0
        public List <Attachment> makeRequest(string placeName, string category)
        {
            Random            rnd     = new Random();
            int               rk      = rnd.Next(keys.Count);
            string            key     = keys[rk];
            List <Attachment> types   = new List <Attachment>();
            string            ts_url  = "https://maps.googleapis.com/maps/api/place/textsearch/json?key=" + key + "&query=" + category + "+in+" + placeName;
            HttpWebRequest    request = (HttpWebRequest)WebRequest.Create(ts_url);//making a http web request

            request.Method = Method.ToString();
            using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
            {
                Stream res_stream = res.GetResponseStream();                                     //creating a response stream to read the response

                StreamReader st_read = new StreamReader(res_stream);                             //crreating a stream reader to read from the response stream
                string       places  = st_read.ReadToEnd();                                      //reading the response stream until the end and assigning to string
                                                                                                 //the response we get is in the form of a json but converted to string when we get, this is serialization
                                                                                                 //once we get the string form of data we convert it into jason to access internal class objects and data,this is called deserialization
                RootObject_ts places_ts = JsonConvert.DeserializeObject <RootObject_ts>(places); //Root object of city deserialization of json data
                int           i         = 0;
                references.Clear();
                foreach (var r1 in places_ts.results)
                {
                    string       typeName    = r1.name;
                    string       rating      = " RATING : " + r1.rating.ToString();
                    string       typeAddress = " ADDRESS : " + r1.formatted_address;
                    PlaceDetails pd          = new PlaceDetails();
                    pd.makeRequest(r1.place_id);
                    string url = pd.getUrl();
                    try
                    {
                        references.Add(r1.photos[0].photo_reference);
                    }
                    catch (Exception e)
                    {
                        references.Add("");
                    }
                    List <CardAction> buttons = new List <CardAction>
                    {
                        new CardAction(ActionTypes.PostBack, " View place and its reviews ", value: "Place Id : " + i + r1.place_id),
                        new CardAction(ActionTypes.OpenUrl, " Get directions to visit place ", value: url)
                    };

                    Attachment heroCard = GetHeroCard(typeName, rating, typeAddress, buttons);
                    types.Add(heroCard);
                    i++;
                    if (i == 5)
                    {
                        break;
                    }
                }
                return(types);
            }
        }