0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Salck APIとを使用してC#でテキストファイルを投稿する

Posted at

はじめに

C#でSlak APIを利用する際に、
Webhookを使用した記事を用いたメッセージ投稿のプログラムは
よくみるものの、
Tokenを使用したプログラムはあまり見られなかったので、
自分への備忘録も兼ねて記事を書きます。

Slack APIについて

基本は使用したいAPIの公式ドキュメントを見てください。
そこに全ては書いてあります。
今回は"files.upload"を使用するので、
以下URL先のドキュメントをしっかりよむ。
https://api.slack.com/methods/files.upload

C#側のコード

以下、今回紹介するソース全文

//外部ファイルより、投稿したいChannelとTokenを取得
string channel = System.Environment.GetEnvironmentVariable("Channel");
string token = System.Environment.GetEnvironmentVariable("Token");

//パラメータにTokenとChannelをセット
var parameters = new NameValueCollection();
parameters["token"] = token;
parameters["channels"] = channel;

var client = new WebClient();
client.QueryString = parameters;

byte[] responseBytes = client.UploadFile("https://slack.com/api/files.upload",Filename);

//結果を取得
String responseString = Encoding.UTF8.GetString(responseBytes);

ReturnValue Return = JsonConvert.DeserializeObject<ReturnValue>(responseString);

Console.WriteLine(Return);
class ReturnValue
{
  public bool ok { get; set; }
  public String error { get; set; }
  public SlackFile file { get; set; }
}
        
class SlackFile
{
 public String id { get; set; }
 public String name { get; set; }
}

外部ファイルより値の取得

System.Environment.GetEnvironmentVariable()では
外部ファイル(今回はAzure Functionで作成したので”localsetting.json”から)
TokenとChannel(正確にはChannelID)から値を引っ張ってきます。
Tokenの取得は以下、
https://dev.classmethod.jp/articles/search-messages-with-slack-api/
ChannelIDの取得は以下を参考にするとわかりやすい
https://qiita.com/unsoluble_sugar/items/603e51106d9632f3ea4f

//外部ファイルより、投稿したいChannelとTokenを取得
string channel = System.Environment.GetEnvironmentVariable("Channel");
string token = System.Environment.GetEnvironmentVariable("Token");

APIに渡す引数のセット

次に、NameValueCollection()をもちいて、
先ほど取得した
"parameters"にTokenとchannels(Slack APIに渡す引数)をセットする。

//パラメータにTokenとChannelをセット
var parameters = new NameValueCollection();
parameters["token"] = token;
parameters["channels"] = channel;

クエリを作成し、WebClientで送信する

WebClientのクエリとして"parameters"をセットした後に、
"client.UploadFile"でSlack APIにクエリを投げかけます。
(変数"Filename"に投稿したいファイルパスが格納されています)

byte[] responseBytes にはSlack APIからの返信がバイトで格納されます。

var client = new WebClient();
client.QueryString = parameters;

byte[] responseBytes = client.UploadFile("https://slack.com/api/files.upload",Filename);

結果をコンソール出力

最後に、送信結果をコンソールに出力すると、便利かも。
(okのパラメータがTrueなら投稿できているはず)

//結果を取得
String responseString = Encoding.UTF8.GetString(responseBytes);

ReturnValue Return = JsonConvert.DeserializeObject<ReturnValue>(responseString);

Console.WriteLine(Return);

最後に

今回で、Slcak APIについて色々わかってきたので、
他も触っていきたいと思う今日この頃

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?