コード例 #1
0
        public static string PushImageMapMessage(string ToUserID, ImagemapMessage Message, string ChannelAccessToken)
        {
            string str1 = "\r\n{{\r\n    'to': '{0}',\r\n    'messages':[\r\n       {1}\r\n    ]\r\n}}\r\n";

            try
            {
                string str2 = "\r\n{{\r\n 'type': 'imagemap',\r\n  'baseUrl': '{0}',\r\n  'altText': '{1}',\r\n 'baseSize': {{\r\n                         'height': {2},\r\n                         'width': {3}\r\n                    }},\r\n  'actions': {4}\r\n}}       \r\n                    ";
                if (Message == null)
                {
                    throw new Exception("Message.");
                }
                if (Message.baseUrl == (Uri)null)
                {
                    throw new Exception("thumbnailImageUrl不得為null.");
                }
                if (Message.actions == null || Message.actions.Count < 1)
                {
                    throw new Exception("actions數量必須>1");
                }
                foreach (ImagemapActionBase action in Message.actions)
                {
                    if (action.GetType().Equals(typeof(ImagemapUriAction)) && (action as ImagemapUriAction).linkUri == (Uri)null)
                    {
                        throw new Exception("ImagemapUriAction 中的 linkUri 不得為null.");
                    }
                }
                string   str3     = JsonConvert.SerializeObject((object)Message.actions);
                string   format   = str2.Replace("'", "\"");
                object[] objArray = new object[5]
                {
                    (object)Message.baseUrl,
                    (object)Message.altText,
                    null,
                    null,
                    null
                };
                int    index1   = 2;
                Size   baseSize = Message.baseSize;
                int    num      = baseSize.Height;
                string str4     = num.ToString();
                objArray[index1] = (object)str4;
                int index2 = 3;
                baseSize = Message.baseSize;
                num      = baseSize.Width;
                string str5 = num.ToString();
                objArray[index2] = (object)str5;
                int    index3 = 4;
                string str6   = str3;
                objArray[index3] = (object)str6;
                string    str7      = string.Format(format, objArray);
                string    s         = string.Format(str1.Replace("'", "\""), (object)ToUserID, (object)str7);
                WebClient webClient = new WebClient();
                webClient.Headers.Clear();
                webClient.Headers.Add("Content-Type", "application/json");
                webClient.Headers.Add("Authorization", "Bearer " + ChannelAccessToken);
                byte[] bytes = Encoding.UTF8.GetBytes(s);
                return(Encoding.UTF8.GetString(webClient.UploadData("https://api.line.me/v2/bot/message/push", bytes)));
            }
            catch (WebException ex)
            {
                using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream()))
                    throw new Exception("PushImageMapMessage API ERROR: " + streamReader.ReadToEnd(), (Exception)ex);
            }
        }
コード例 #2
0
ファイル: Bot.cs プロジェクト: ingted/d9sys_bot
 public string PushMessage(string ToUserID, ImagemapMessage Message)
 {
     return(Utility.PushImageMapMessage(ToUserID, Message, this.channelAccessToken));
 }