public static string PushTemplateMessage(string ToUserID, ButtonsTemplate 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': 'buttons',\r\n 'thumbnailImageUrl': '{1}',\r\n 'title': '{2}',\r\n 'text': '{3}',\r\n 'actions': {4}\r\n }}\r\n}} \r\n "; if (TemplateMessage == null) { throw new Exception("TemplateMessage不得為null."); } if (TemplateMessage.thumbnailImageUrl == (Uri)null) { throw new Exception("thumbnailImageUrl不得為null."); } if (TemplateMessage.actions == null || TemplateMessage.actions.Count < 1 || TemplateMessage.actions.Count > 4) { throw new Exception("actions數量必須是1-4之間"); } 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.thumbnailImageUrl.ToString(), (object)TemplateMessage.title, (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(ButtonsTemplate) API ERROR: " + streamReader.ReadToEnd(), (Exception)ex); } }
public string PushMessage(string ToUserID, ButtonsTemplate Message) { return(Utility.PushTemplateMessage(ToUserID, Message, this.channelAccessToken)); }