0
0

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.

Slackで特定のメッセージを取得するby C#

Posted at

はじめに

Slack APIで自動でメッセージを投稿して、
そのメッセージを別のチャンネルにメッセージを引用したので
その覚書

やりたいこと

chat.postMessage APIを利用して投稿した値のリクエストに対するレスポンスからtsを取得。

その後、chat.getPermalink APIのmessage_tsとして設定することにより
レスポンスとしてメッセージURLを取得できる。
その値をchat.postMe APIで投稿する。

今回はchat.getPermalink APIで設定する部分を紹介する。

公式ドキュメントは以下。
必要なあるいは設定できる引数やレスポンスの形式が記載されているので
APIを使う前に目を通した方が良い。

・chat.postMessage
https://api.slack.com/methods/chat.postMessage

・chat.getPermalink
https://api.slack.com/methods/chat.getPermalink

ソース

public class Responce
{ 
  public string permalink{get; set; }
}
public string Main()
{
  var parameters = new NameValueCollection();

  //クエリ作成
  parameters.ADD("token",["トークン"]);
  parameters.ADD("channel",["チャンネルID"]);
  parameters.ADD("message_ts",["取得したいメッセージのts"]);

  using(var client = new WebClient())
  {
    //リクエストを送信し、レスポンスを取得
    byte[] responceByte = client.Upload.Values("https://slack.com/api/chat.getPermalink",parameters)

    string responceStr = Encoding.UTF8.GetDtring(responceByte);
    Responce responce = JsonConvert.DeserializeObject<Responce>(responceStr);

    //取得したメッセージのURLを返す
    return (responce.permalink);

}

Content Type: application/x-www-form-urlencodedの形式で
トークン、チャンネルID、tsを引数としてリクエストを送信してやれば良い。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?