Esempio n. 1
0
        public static string PushTemplateMessage(string ToUserID, CarouselTemplate TemplateMessage, 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': 'template',\r\n  'altText': '{0}',\r\n  'template': {{\r\n      'type': 'carousel',\r\n      'columns': {1}\r\n  }}\r\n}}       \r\n                    ";
                if (TemplateMessage == null)
                {
                    throw new Exception("TemplateMessage不得為null.");
                }
                if (TemplateMessage.columns == null || TemplateMessage.columns.Count < 1 || TemplateMessage.columns.Count > 5)
                {
                    throw new Exception("columns數量必須是1-5之間");
                }
                foreach (Column column in TemplateMessage.columns)
                {
                    if (column.actions == null)
                    {
                        throw new Exception("actions數量必須是1-3之間");
                    }
                    if (column.actions.Count < 1 || column.actions.Count > 3)
                    {
                        throw new Exception("actions數量必須是1-3之間");
                    }
                    foreach (TemplateActionBase action in column.actions)
                    {
                        if (action.GetType().Equals(typeof(UriActon)) && (action as UriActon).uri == (Uri)null)
                        {
                            throw new Exception("uriAction 中的 Url不得為null.");
                        }
                    }
                }
                string    str3      = JsonConvert.SerializeObject((object)TemplateMessage.columns);
                string    str4      = string.Format(str2.Replace("'", "\""), (object)TemplateMessage.altText, (object)str3);
                string    s         = string.Format(str1.Replace("'", "\""), (object)ToUserID, (object)str4);
                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("PushTemplateMessage API(CarouselTemplate) ERROR: " + streamReader.ReadToEnd(), (Exception)ex);
            }
        }
Esempio n. 2
0
 public string PushMessage(string ToUserID, CarouselTemplate Message)
 {
     return(Utility.PushTemplateMessage(ToUserID, Message, this.channelAccessToken));
 }