Beispiel #1
0
        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        static public SendResult SendMessage(JMessage msg)
        {
            var    oauthDataProivder = SiteManager.Get <IOAuthDataProvider>();
            string url         = String.Format(messageUrl, AccessToken);
            var    users       = oauthDataProivder.GetOAuthUserId(ApiHelper.ProviderName, msg.SendToIds);
            var    textCardMsg = new TextCardMessage
            {
                AgentId = AgentId,

                TextCard = new TextCard
                {
                    Title       = msg.Title,
                    ButtonText  = "查看更多",
                    Description = String.IsNullOrWhiteSpace(msg.Content) ? msg.Title : msg.Content,
                    Url         = CreateOAuthUrl(msg.Url),
                },
                ToUser = String.Join("|", users),
            };
            string result = PostJson(url, textCardMsg);

            return(new SendResult
            {
                MessageId = msg.Id
            });
        }
Beispiel #2
0
        //发送消息
        public static string SendMessage(Receiver receiver, string msgtype, Content content, string media_id, int safe, string token)
        {
            int             agentid     = Convert.ToInt32(ConfigurationManager.AppSettings["agentid"]);
            string          result      = "";
            string          messagejson = "";
            Text            text;
            TextMessage     textmessage;
            Articles        articles;
            List <Articles> listarticle;

            switch (msgtype)
            {
            case "text":
                text                = new Text();
                textmessage         = new TextMessage();
                text.content        = content.text;
                textmessage.touser  = receiver.touser;
                textmessage.toparty = receiver.toparty;
                textmessage.totag   = receiver.totag;
                textmessage.msgtype = msgtype;
                textmessage.agentid = agentid;
                textmessage.text    = text;
                messagejson         = JsonConvert.SerializeObject(textmessage);
                result              = PostJson(token, messagejson);
                break;

            case "image":
                Image image = new Image();
                image.media_id = media_id;
                ImageMessage imagemessage = new ImageMessage();
                imagemessage.touser  = receiver.touser;
                imagemessage.msgtype = msgtype;
                imagemessage.agentid = agentid;
                imagemessage.image   = image;
                messagejson          = JsonConvert.SerializeObject(imagemessage);
                result = PostJson(token, messagejson);
                break;

            case "voice":
                Voice voice = new Voice();
                voice.media_id = media_id;
                VoiceMessage voicemessage = new VoiceMessage();
                voicemessage.touser  = receiver.touser;
                voicemessage.msgtype = msgtype;
                voicemessage.agentid = agentid;
                voicemessage.voice   = voice;
                messagejson          = JsonConvert.SerializeObject(voicemessage);
                result = PostJson(token, messagejson);
                break;

            case "file":
                Files file = new Files();
                file.media_id = media_id;
                FilesMessage filemessage = new FilesMessage();
                filemessage.touser  = receiver.touser;
                filemessage.msgtype = msgtype;
                filemessage.agentid = agentid;
                filemessage.file    = file;
                messagejson         = JsonConvert.SerializeObject(filemessage);
                result = PostJson(token, messagejson);
                break;

            case "video":
                Video video = new Video();
                video.media_id    = media_id;
                video.title       = content.title;
                video.description = content.description;
                VideoMessage videomessage = new VideoMessage();
                videomessage.touser  = receiver.touser;
                videomessage.msgtype = msgtype;
                videomessage.agentid = agentid;
                videomessage.video   = video;
                messagejson          = JsonConvert.SerializeObject(videomessage);
                result = PostJson(token, messagejson);
                break;

            case "textcard":
                TextCard textcard = new TextCard();
                textcard.title       = content.title;
                textcard.description = content.description;
                textcard.url         = content.url;
                textcard.btntxt      = content.btntxt;
                TextCardMessage textcardmessage = new TextCardMessage();
                textcardmessage.touser   = receiver.touser;
                textcardmessage.msgtype  = msgtype;
                textcardmessage.agentid  = agentid;
                textcardmessage.textcard = textcard;
                messagejson = JsonConvert.SerializeObject(textcardmessage);
                result      = PostJson(token, messagejson);
                break;

            case "news":
                News news = new News();
                articles             = new Articles();
                articles.title       = content.title;
                articles.description = content.description;
                articles.url         = content.url;
                articles.picurl      = content.picurl;
                articles.btntxt      = content.btntxt;
                listarticle          = new List <Articles>();
                listarticle.Add(articles);
                news.articles = listarticle;
                NewsMessage newsmessage = new NewsMessage();
                newsmessage.touser  = receiver.touser;
                newsmessage.msgtype = msgtype;
                newsmessage.agentid = agentid;
                newsmessage.news    = news;
                messagejson         = JsonConvert.SerializeObject(newsmessage);
                result = PostJson(token, messagejson);
                break;

            case "mpnews":
                MpNews mpnews = new MpNews();
                articles                    = new Articles();
                articles.title              = content.title;
                articles.thumb_media_id     = media_id;
                articles.author             = content.author;
                articles.content_source_url = content.url;
                articles.content            = content.content;
                articles.digest             = content.description;
                listarticle                 = new List <Articles>();
                listarticle.Add(articles);
                mpnews.articles = listarticle;
                MpNewsMessage mpnewsmessage = new MpNewsMessage();
                mpnewsmessage.touser  = receiver.touser;
                mpnewsmessage.msgtype = msgtype;
                mpnewsmessage.agentid = agentid;
                mpnewsmessage.mpnews  = mpnews;
                messagejson           = JsonConvert.SerializeObject(mpnewsmessage);
                result = PostJson(token, messagejson);
                break;

            default:
                text                = new Text();
                textmessage         = new TextMessage();
                text.content        = content.text;
                textmessage.touser  = receiver.touser;
                textmessage.msgtype = msgtype;
                textmessage.agentid = agentid;
                textmessage.text    = text;
                messagejson         = JsonConvert.SerializeObject(textmessage);
                result              = PostJson(token, messagejson);
                break;
            }
            return(result);
        }