こんにちは。
Windows10でC#のアプリケーションを開発しています。
Visual Studio 2017 Communityを使っています。
###前提・実現したいこと
apiでslackに投稿したいです。
###試したこと
slackに自分のチャンネルを用意。
TOKENを取得。
channnel idを取得。
hooksで投稿(成功)。
###発生している問題・エラーメッセージ
{"ok":false,"error":"channel_not_found"}
###該当のソースコード
C#
1using Newtonsoft.Json; 2using System; 3using System.Collections.Specialized; 4using System.Net; 5using System.Text; 6using System.Windows; 7 8//TOKEN発行 https://api.slack.com/custom-integrations/legacy-tokens 9//channelID http://qiita.com/Yinaura/items/bd28c7b9ef614696fb7e 10namespace SlackAPI 11{ 12 public partial class MainWindow : Window 13 { 14 public MainWindow() 15 { 16 InitializeComponent(); 17 } 18 19 static string token = "xoxp-token";//token 20 static string urlWithAccessToken = "https://slack.com/api/chat.postMessage?token=" + token; 21 private void Button_Click(object sender, RoutedEventArgs e) 22 { 23 SlackClient client = new SlackClient(urlWithAccessToken); 24 try 25 { 26 client.PostMessage(username: "cancat", text: "THIS IS A TEST MESSAGE!", channel: "CANNELID"); 27 } 28 catch (Exception exception) 29 { 30 } 31 } 32 33 public class SlackClient 34 { 35 private readonly Uri uri; 36 private readonly Encoding encoding = new UTF8Encoding(); 37 38 public SlackClient(string urlWithAccessToken) 39 { 40 uri = new Uri(urlWithAccessToken); 41 } 42 43 public void PostMessage(string text, string username = null, string channel = null) 44 { 45 Message payload = new Message() 46 { 47 Channel = channel, 48 Username = username, 49 Text = text 50 }; 51 52 string payloadJson = JsonConvert.SerializeObject(payload); 53 using (WebClient client = new WebClient()) 54 { 55 NameValueCollection data = new NameValueCollection(); 56 data["payload"] = payloadJson; 57 58 var response = client.UploadValues(uri, "POST", data); 59 string responseText = encoding.GetString(response); 60 MessageBox.Show(responseText + "\r\n" + payload.Channel); 61 } 62 } 63 } 64 65 public class Message 66 { 67 [JsonProperty("channel")] 68 public string Channel { get; set; } 69 70 [JsonProperty("username")] 71 public string Username { get; set; } 72 73 [JsonProperty("text")] 74 public string Text { get; set; } 75 public string Icon { get; set; } = ":ghost:"; 76 } 77 } 78}
###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio Community 2017
Version 15.0.26228.9 D15RTWSVC
Microsoft .NET Framework
Version 4.6.01586
です。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。