Beispiel #1
0
        private WechatMessage ConvertMessage(ITurnContext context, IMessageActivity activity)
        {
            //The connector could handle this conversion instead...
            var wechatMessage = new WechatMessage
            {
                ToUserName = context.GetChannelUserId()
            };

            var richCards = activity.Attachments?.Where(att => att.ContentType.Equals("application/vnd.microsoft.card.hero", System.StringComparison.InvariantCultureIgnoreCase));

            if (richCards?.Count() > 0)
            {
                wechatMessage.MessageType = WechatMessageTypes.RICH_MEDIA;
                wechatMessage.Articles    = richCards.Select(att =>
                {
                    var richCard = att.Content as HeroCard;
                    return(new WechatArticle
                    {
                        Title = richCard.Title,
                        Description = richCard.Subtitle ?? richCard.Text,
                        PicUrl = richCard.Images?.FirstOrDefault()?.Url ?? string.Empty
                    });
                });

                return(wechatMessage);
            }

            var image = activity.Attachments?.FirstOrDefault(att => att.ContentType.Contains("image"));

            if (image != null)
            {
                wechatMessage.MessageType = WechatMessageTypes.IMAGE;
                wechatMessage.MediaId     = image.ContentUrl;
                return(wechatMessage);
            }

            wechatMessage.MessageType = WechatMessageTypes.TEXT;
            wechatMessage.Content     = activity.Text;
            return(wechatMessage);
        }