Beispiel #1
0
        public static string PushTemplateMessage(string ToUserID, ConfirmTemplate 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': 'confirm',\r\n      'text': '{1}',\r\n      'actions': {2}\r\n  }}\r\n}}       \r\n                    ";
                if (TemplateMessage == null)
                {
                    throw new Exception("TemplateMessage不得為null.");
                }
                if (TemplateMessage.actions == null || TemplateMessage.actions.Count < 1 || TemplateMessage.actions.Count > 2)
                {
                    throw new Exception("actions數量必須是1-2之間");
                }
                foreach (TemplateActionBase action in TemplateMessage.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.actions);
                string    str4      = string.Format(str2.Replace("'", "\""), (object)TemplateMessage.altText, (object)TemplateMessage.text, (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(ConfirmTemplate) API ERROR: " + streamReader.ReadToEnd(), (Exception)ex);
            }
        }
Beispiel #2
0
 public string PushMessage(string ToUserID, ConfirmTemplate Message)
 {
     return(Utility.PushTemplateMessage(ToUserID, Message, this.channelAccessToken));
 }