Пример #1
0
        /// <summary>
        /// 发送文本消息
        /// </summary>
        /// <param name="ToUserName"></param>
        /// <param name="Content"></param>
        public void SendMsg(string ToUserName, string Content)
        {
            Contact ToUser = WechatAPIService.GetContact(ToUserName);

            API.Message msg = new API.Message()
            {
                MsgType = 1,
                Content = Content,
                IsSend  = true,
                Remote  = ToUser,
                Mime    = WechatAPIService.Self,
            };
            Task <SendMsgResponse> task = api.SendMsgAsync(ToUserName, Content);

            task.ContinueWith((obj) =>
            {
                SendMsgResponse res = obj.Result;
                if (res != null && !string.IsNullOrEmpty(res.MsgID))
                {
                    MessageDao MsgDao = DaoMaster.GetSession().GetMessageDao();
                    msg.MsgId         = res.MsgID;
                    MsgDao.InsertMessage(msg, openContact.Seq);
                }
            });
        }
Пример #2
0
        private void LoadMessageHistory()
        {
            if (openContact == null)
            {
                return;
            }
            string             Seq     = openContact.Seq;
            MessageDao         MsgDao  = DaoMaster.GetSession().GetMessageDao();
            List <API.Message> message = MsgDao.GetMessage(Seq);

            m_SyncContext.Post(UpdateMessageList, message);
        }
Пример #3
0
        public static Message CreateMessage(AddMsg msg)
        {
            //MsgType
            //1   文本消息
            //3   图片消息
            //34  语音消息
            //37  VERIFYMSG
            //40  POSSIBLEFRIEND_MSG
            //42  共享名片
            //43  视频通话消息
            //47  动画表情
            //48  位置消息
            //49  分享链接
            //50  VOIPMSG
            //51  微信初始化消息
            //52  VOIPNOTIFY
            //53  VOIPINVITE
            //62  小视频
            //9999    SYSNOTICE
            //10000   系统消息
            //10002   撤回消息

            bool    isSend = IsSend(msg.FromUserName);
            Contact remote = (isSend) ? WechatAPIService.GetContact(msg.ToUserName) : WechatAPIService.GetContact(msg.FromUserName);

            Message ret = new Message();

            switch (msg.MsgType)
            {
            case 1:
                ret.Content = msg.Content;
                break;

            case 3:

                string sevePath = Path.Combine(m_Service.CachePath, DateTime.Now.Ticks + ".bmp");
                ret.fileName = sevePath;
                ret.FileSize = msg.FileSize;
                string thumbnail = string.Format("/cgi-bin/mmwebwx-bin/webwxgetmsgimg?MsgID={0}&type=slave", msg.MsgId);
                ret.Content = string.Format("/cgi-bin/mmwebwx-bin/webwxgetmsgimg?MsgID={0}", msg.MsgId);

                Image image = m_Service.GetMsgImage(thumbnail);
                if (image == null)
                {
                    break;
                }
                image.Save(sevePath);
                break;

            case 34:
                sevePath        = Path.Combine(m_Service.CachePath, DateTime.Now.Ticks + ".MP3");
                ret.fileName    = sevePath;
                ret.FileSize    = msg.FileSize;
                ret.VoiceLength = msg.VoiceLength;
                string url = string.Format("/cgi-bin/mmwebwx-bin/webwxgetvoice?MsgID={0}", msg.MsgId);
                m_Service.GetMsgFile(url, sevePath);

                break;

            case 47:
                //下载图片
                sevePath     = Path.Combine(m_Service.CachePath, Path.GetRandomFileName());
                ret.fileName = sevePath;
                ret.FileSize = msg.FileSize;
                if (string.IsNullOrEmpty(msg.Content))
                {
                    break;
                }

                url = XMLTools.GetImageUrl(msg.Content);
                if (string.IsNullOrEmpty(url))
                {
                    break;
                }
                image = m_Service.GetImage(url);
                if (image == null)
                {
                    break;
                }
                image.Save(sevePath);
                break;

            default:
                ret.Content = msg.Content;
                break;
            }
            if (ret == null)
            {
                ret = new Message();
            }
            ret.MsgType       = msg.MsgType;
            ret.IsSend        = isSend;
            ret.Remote        = remote;
            ret.FromContactID = msg.FromUserName;
            ret.ToContactD    = msg.ToUserName;
            ret.Mime          = WechatAPIService.Self;
            ret.MsgId         = msg.MsgId;
            if (msg.MsgType != 51)
            {
                DaoMaster.GetSession().GetMessageDao().InsertMessage(ret, remote.Seq);
            }

            return(ret);
        }