Esempio n. 1
0
        public async Task <IActionResult> SendVoiceMessage([FromBody] VoiceMessageModel model)
        {
            if (model == null || string.IsNullOrWhiteSpace(model.DestinationNumber) ||
                (string.IsNullOrWhiteSpace(model.SsmlMessage) && string.IsNullOrWhiteSpace(model.PlainTextMessage)))
            {
                return(BadRequestResult("Voice message"));
            }

            if (!ValidationHelper.IsValidPhoneNumber(model.DestinationNumber))
            {
                return(BadRequestResult("Voice message"));
            }

            using (AmazonPinpointSMSVoiceClient client = new AmazonPinpointSMSVoiceClient(_awsCredentials, RegionEndpoint.GetBySystemName(_awsSettings.Region)))
            {
                SendVoiceMessageRequest sendVoiceMessageRequest = new SendVoiceMessageRequest
                {
                    DestinationPhoneNumber = model.DestinationNumber,
                    OriginationPhoneNumber = _awsSettings.AwsTextVoiceMessage.OriginationNumber,
                    Content = new VoiceMessageContent
                    {
                        SSMLMessage = string.IsNullOrWhiteSpace(model.SsmlMessage) ? null : new SSMLMessageType
                        {
                            LanguageCode = model.LanguageCode ?? _awsSettings.LanguageCode, //en-US is ideal for US.
                            VoiceId      = model.VoiceId ?? "Matthew",
                            Text         = model.SsmlMessage
                        },
                        PlainTextMessage = !string.IsNullOrWhiteSpace(model.SsmlMessage) ? null : new PlainTextMessageType
                        {
                            LanguageCode = model.LanguageCode ?? _awsSettings.LanguageCode, //en-US is ideal for US.
                            VoiceId      = model.VoiceId ?? "Matthew",
                            Text         = model.PlainTextMessage
                        }
                    }
                };
                try
                {
                    SendVoiceMessageResponse response = await client.SendVoiceMessageAsync(sendVoiceMessageRequest);

                    if (response.HttpStatusCode != HttpStatusCode.OK)
                    {
                        return(BadRequestResult("Voice message"));
                    }
                }
                catch
                {
                    return(BadRequestResult("Voice message"));
                }
                return(new OkObjectResult(new { success = true, message = "Voice message sent." }));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 发送语音消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SendVoiceMessage(VoiceMessageModel model)
        {
            string data = JsonHelper.JsonSerializer(model);//转化json数据

            return(CommSendMessage(data));
        }
Esempio n. 3
0
    /// <summary>
    /// 回复消息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        string         data    = string.Empty;
        SendMessageApi sendApi = new SendMessageApi();

        #region 文本消息
        if (hidMsgType.Value == CommonEnum.enumMsgType.text.ToString())//文本消息
        {
            TextMessageModel txtModel = new TextMessageModel();
            txtModel.msgtype      = hidMsgType.Value;                            //消息类型
            txtModel.touser       = getmodel.TMIOpenId;                          //接收人
            txtModel.msgrelative  = getmodel.TMIMsgRelative;                     //消息唯一标识
            txtModel.msgsource    = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源
            txtModel.text.content = txtContent.Text.Trim();
            if (!sendApi.SendTextMessage(txtModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(txtModel);
        }
        #endregion

        #region 图片消息
        else if (hidMsgType.Value == CommonEnum.enumMsgType.image.ToString())//图片消息
        {
            ImageMessageModel imgModel = new ImageMessageModel();
            string            fileName = GetFileName(Fileimg.PostedFile, "imgMsg");
            if (string.IsNullOrEmpty(fileName))
            {
                Response.Write("<script>alert('上传文件为空')</script>");
                return;
            }
            imgModel.msgtype        = hidMsgType.Value;
            imgModel.touser         = getmodel.TMIOpenId;
            imgModel.msgrelative    = getmodel.TMIMsgRelative;                                                          //消息唯一标识
            imgModel.msgsource      = CommonEnum.enumMsgSource.send.GetHashCode();                                      //消息来源
            imgModel.image.media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.image.ToString()); //上传多媒体返回media_Id
            if (!sendApi.SendImageMessage(imgModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(imgModel);
        }
        #endregion

        #region 语音消息
        else if (hidMsgType.Value == CommonEnum.enumMsgType.voice.ToString())//语音消息
        {
            VoiceMessageModel vocModel = new VoiceMessageModel();
            string            fileName = GetFileName(Filevoice.PostedFile, "voiceMsg");
            if (string.IsNullOrEmpty(fileName))
            {
                Response.Write("<script>alert('上传文件为空')</script>");
                return;
            }
            vocModel.msgtype        = hidMsgType.Value;
            vocModel.touser         = getmodel.TMIOpenId;                                                               //接收人
            vocModel.msgrelative    = getmodel.TMIMsgRelative;                                                          //消息唯一标识
            vocModel.msgsource      = CommonEnum.enumMsgSource.send.GetHashCode();                                      //消息来源
            vocModel.voice.media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.voice.ToString()); //上传多媒体返回media_Id
            if (!sendApi.SendVoiceMessage(vocModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(vocModel);
        }
        #endregion

        #region 视频消息
        else if (hidMsgType.Value == CommonEnum.enumMsgType.video.ToString())//视频消息
        {
            VideoMessageModel vdoModel = new VideoMessageModel();
            string            fileName = GetFileName(FileVideo.PostedFile, "videoMsg");//上传返回名称
            if (string.IsNullOrEmpty(fileName))
            {
                Response.Write("<script>alert('上传文件为空')</script>");
                return;
            }
            vdoModel.msgtype              = hidMsgType.Value;
            vdoModel.touser               = getmodel.TMIOpenId;                                                               //接收人
            vdoModel.msgrelative          = getmodel.TMIMsgRelative;                                                          //消息唯一标识
            vdoModel.msgsource            = CommonEnum.enumMsgSource.send.GetHashCode();                                      //消息来源
            vdoModel.video.media_id       = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.video.ToString()); //上传多媒体返回media_Id
            vdoModel.video.thumb_media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.thumb.ToString()); //上传多媒体返回media_Id
            vdoModel.video.title          = title.Text;
            vdoModel.video.description    = description.Text;
            if (!sendApi.SendVideoMessage(vdoModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(vdoModel);
        }
        #endregion

        #region 音乐消息
        else if (hidMsgType.Value == CommonEnum.enumMsgType.music.ToString())//音乐消息
        {
            MusicMessageModel musModel = new MusicMessageModel();
            string            fileName = GetFileName(FileMusic.PostedFile, "musicMsg");
            if (string.IsNullOrEmpty(fileName))
            {
                Response.Write("<script>alert('上传文件为空')</script>");
                return;
            }
            musModel.msgtype              = hidMsgType.Value;
            musModel.touser               = getmodel.TMIOpenId;                          //接收人
            musModel.msgrelative          = getmodel.TMIMsgRelative;                     //消息唯一标识
            musModel.msgsource            = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源
            musModel.music.musicurl       = musicUrl.Text.Trim();
            musModel.music.hqmusicurl     = hqMusicUrl.Text;
            musModel.music.title          = title.Text;
            musModel.music.description    = description.Text;
            musModel.music.thumb_media_id = CommMsgParam.UploadMultimedia(fileName, CommonEnum.enumMsgType.thumb.ToString());//上传多媒体返回media_Id;
            if (!sendApi.SendMusicMessage(musModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(musModel);
        }
        #endregion

        #region 图文消息
        else if (hidMsgType.Value == CommonEnum.enumMsgType.news.ToString())//图文消息
        {
            NewsMessageModel newsModel = new NewsMessageModel();
            newsModel.msgtype     = hidMsgType.Value;
            newsModel.touser      = getmodel.TMIOpenId;                          //接收人
            newsModel.msgrelative = getmodel.TMIMsgRelative;                     //消息唯一标识
            newsModel.msgsource   = CommonEnum.enumMsgSource.send.GetHashCode(); //消息来源
            List <NewsArticles> list = new List <NewsArticles>();
            string   newslist        = hidnewsType.Value.TrimEnd('|');
            string[] newsListArr     = newslist.Split('|');
            string   picUrls         = string.Empty;//获取所有的图片链接
            for (int i = 0; i < newsListArr.Length; i++)
            {
                NewsArticles articles = new NewsArticles();
                string[]     listArr  = newsListArr[i].Split(',');
                articles.title       = listArr[0]; //图文标题
                articles.url         = listArr[1]; //点击跳转的链接
                articles.picurl      = listArr[2]; //图文消息的链接
                articles.description = listArr[3]; //描述
                list.Add(articles);
                picUrls += list[0] + ",";
            }
            newsModel.news.articles = list;
            if (!sendApi.SendNewsMessage(newsModel))
            {
                Response.Write("<script>alert('发送失败')</script>");
                return;
            }
            data = JsonHelper.JsonSerializer(newsModel);
        }
        #endregion

        SaveMessage(data);//保存消息
    }