private static bool IsMediaWithCaption(InfobipOmniWhatsAppMessage message)
        {
            var mediaWithCaptionUrls = new List <string> {
                message.FileUrl, message.ImageUrl, message.VideoUrl
            };

            return(mediaWithCaptionUrls.Any(x => !string.IsNullOrWhiteSpace(x)));
        }
        private static void HandleText(IMessageActivity activity, IList <InfobipOmniWhatsAppMessage> messages)
        {
            var mediaMessage = messages.FirstOrDefault();

            if (mediaMessage != null && IsMediaWithCaption(mediaMessage))
            {
                mediaMessage.Text = activity.Text;
                return;
            }

            var message = new InfobipOmniWhatsAppMessage {
                Text = activity.Text
            };

            messages.Insert(0, message);
        }
 private void CheckAudioMessage(InfobipOmniWhatsAppMessage whatsAppMessage, Attachment attachment)
 {
     Assert.NotNull(whatsAppMessage);
     Assert.Null(whatsAppMessage.Text);
     Assert.Null(whatsAppMessage.FileUrl);
     Assert.Null(whatsAppMessage.LocationName);
     Assert.Equal(whatsAppMessage.AudioUrl, attachment.ContentUrl);
     Assert.Null(whatsAppMessage.Address);
     Assert.Null(whatsAppMessage.Latitude);
     Assert.Null(whatsAppMessage.Longitude);
     Assert.Null(whatsAppMessage.ImageUrl);
     Assert.Null(whatsAppMessage.VideoUrl);
     Assert.Null(whatsAppMessage.TemplateNamespace);
     Assert.Null(whatsAppMessage.TemplateData);
     Assert.Null(whatsAppMessage.TemplateName);
     Assert.Null(whatsAppMessage.Language);
     Assert.Null(whatsAppMessage.MediaTemplateData);
 }
 private static void SetMediaUrl(string contentType, InfobipOmniWhatsAppMessage message, Attachment attachment)
 {
     if (contentType.Contains("audio"))
     {
         message.AudioUrl = attachment.ContentUrl;
     }
     else if (contentType.Contains("image"))
     {
         message.ImageUrl = attachment.ContentUrl;
     }
     else if (contentType.Contains("video"))
     {
         message.VideoUrl = attachment.ContentUrl;
     }
     else
     {
         message.FileUrl = attachment.ContentUrl;
     }
 }
        private static void HandleEntities(IActivity activity, ICollection <InfobipOmniWhatsAppMessage> messages)
        {
            foreach (var entity in activity.Entities)
            {
                if (entity.Type == InfobipEntityType.CallbackData)
                {
                    continue;
                }

                var geoCoordinates = entity.GetAs <GeoCoordinates>();
                if (geoCoordinates.Type == nameof(GeoCoordinates))
                {
                    var message = new InfobipOmniWhatsAppMessage();
                    message.SetLocation(geoCoordinates);
                    messages.Add(message);
                    continue;
                }

                var place = entity.GetAs <Place>();
                if (place.Type == nameof(Place))
                {
                    if (place.Address != null && place.Address.GetType() != typeof(string))
                    {
                        throw new Exception("For Place object Address can only be string");
                    }

                    var geoEntity = JsonConvert.DeserializeObject <GeoCoordinates>(JsonConvert.SerializeObject(place.Geo));
                    if (geoEntity == null || geoEntity.Type != nameof(GeoCoordinates))
                    {
                        throw new Exception("For Place object required param is Geo and must be type of GeoCoordinates");
                    }

                    var newMessage = new InfobipOmniWhatsAppMessage();
                    newMessage.Address = place.Address?.ToString();
                    newMessage.SetLocation(geoEntity);
                    messages.Add(newMessage);
                    continue;
                }

                throw new Exception("Supported entities are GeoCoordinate and Place.");
            }
        }
        private void CheckLocationMessage(InfobipOmniWhatsAppMessage whatsAppMessage, GeoCoordinates geoCoordinate)
        {
            Assert.NotNull(geoCoordinate);

            Assert.NotNull(whatsAppMessage);
            Assert.Null(whatsAppMessage.Text);
            Assert.Null(whatsAppMessage.FileUrl);
            Assert.Equal(whatsAppMessage.LocationName, geoCoordinate.Name);
            Assert.Null(whatsAppMessage.AudioUrl);
            Assert.Null(whatsAppMessage.Address);
            Assert.Equal(whatsAppMessage.Latitude, geoCoordinate.Latitude);
            Assert.Equal(whatsAppMessage.Longitude, geoCoordinate.Longitude);
            Assert.Null(whatsAppMessage.ImageUrl);
            Assert.Null(whatsAppMessage.VideoUrl);
            Assert.Null(whatsAppMessage.TemplateNamespace);
            Assert.Null(whatsAppMessage.TemplateData);
            Assert.Null(whatsAppMessage.TemplateName);
            Assert.Null(whatsAppMessage.Language);
            Assert.Null(whatsAppMessage.MediaTemplateData);
        }
        private static void HandleAttachments(IMessageActivity activity, ICollection <InfobipOmniWhatsAppMessage> messages)
        {
            foreach (var attachment in activity.Attachments)
            {
                ValidateContentType(attachment);
                var contentType = attachment.ContentType.ToLower();
                var message     = new InfobipOmniWhatsAppMessage();

                if (contentType == InfobipWhatsAppAttachmentContentTypes.WhatsAppMessageTemplate && attachment.Content is InfobipWhatsAppTemplateMessage templateMessage)
                {
                    message.SetTemplate(templateMessage);
                }
                else
                {
                    ValidateContentUrl(attachment);
                    SetMediaUrl(contentType, message, attachment);
                }

                messages.Add(message);
            }
        }
        private void CheckLocationMessage(InfobipOmniWhatsAppMessage whatsAppMessage, Place place)
        {
            Assert.NotNull(place);

            var geoCoordinate = JsonConvert.DeserializeObject <GeoCoordinates>(JsonConvert.SerializeObject(place.Geo));

            Assert.NotNull(geoCoordinate);

            Assert.NotNull(whatsAppMessage);
            Assert.Null(whatsAppMessage.Text);
            Assert.Null(whatsAppMessage.FileUrl);
            Assert.Equal(whatsAppMessage.LocationName, geoCoordinate.Name);
            Assert.Null(whatsAppMessage.AudioUrl);
            Assert.Equal(whatsAppMessage.Address, place.Address);
            Assert.Equal(whatsAppMessage.Latitude, geoCoordinate.Latitude);
            Assert.Equal(whatsAppMessage.Longitude, geoCoordinate.Longitude);
            Assert.Null(whatsAppMessage.ImageUrl);
            Assert.Null(whatsAppMessage.VideoUrl);
            Assert.Null(whatsAppMessage.TemplateNamespace);
            Assert.Null(whatsAppMessage.TemplateData);
            Assert.Null(whatsAppMessage.TemplateName);
            Assert.Null(whatsAppMessage.Language);
            Assert.Null(whatsAppMessage.MediaTemplateData);
        }