Esempio n. 1
0
 public async void TranslateTest()
 {
     var result = await _giphy.TranslateIntoGif(new TranslateParameter()
     {
         Phrase = "superman"
     });
 }
Esempio n. 2
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                var match = GiphyRegex.Match(activity.Text ?? string.Empty);
                if (match.Success)
                {
                    var searchText = match.Groups["search"].Value.Trim();

                    telemetryClient?.TrackEvent("BotRequest", new Dictionary <string, string> {
                        { "Search", searchText }
                    });

                    var result =
                        await giphyManager.TranslateIntoGif(new TranslateParameter { Phrase = searchText, Rating = Rating.Pg });

                    Activity reply;
                    if (result == null)
                    {
                        reply = activity.CreateReply("Sorry giphy sucks!");
                    }
                    else
                    {
                        var fakeFileName = $"{searchText.Replace(' ', '_')}.{result.Data.Type}";
                        reply = activity.CreateReply();

                        // return our reply to the user
                        if (reply != null)
                        {
                            reply.Attachments = new List <Attachment>
                            {
                                new Attachment
                                {
                                    ContentType = $"image/{result.Data.Type}",
                                    ContentUrl  = result.Data.Images.Original.Url,
                                    Name        = fakeFileName
                                }
                            };
                        }
                    }

                    await connector.Conversations.ReplyToActivityAsync(reply);
                }
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }