private static void MainLoop(ISlackRealTimeMessaging slackRtm, SlackMessageHandler handler, SlackApi slackApi, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var nextMessage = slackRtm.Receive(cancellationToken); while (!nextMessage.Wait(TimeSpan.FromSeconds(10))) { var tickResult = handler.HandleTimerTick(); Console.Write("."); DoResponse(slackApi, tickResult, cancellationToken); } var messageResult = handler.Handle(nextMessage.Result); DoResponse(slackApi, messageResult, cancellationToken); } }
public async Task Operation(HttpRequestMessage request) { // =========================== // リクエストの取得・整形 // =========================== NameValueCollection data = await GetBody(request); // 引数の値 var method = data["text"]; // =========================== // 引数の値で処理を分ける // =========================== switch (method) { // =========================== // リポジトリ登録 // =========================== case "set": { // ==================================== // リポジトリ登録用ダイアログモデル作成 // ==================================== var model = CreateDialogModelForSetRespotory(data["trigger_id"]); // ============================= // ダイアログAPI実行 // ============================= await SlackApi.ExecutePostApiAsJson(model, "https://slack.com/api/dialog.open", data["team_id"]); break; } // =========================== // 登録リポジトリ照会 // =========================== case "get": { await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "登録リポジトリのURLを照会します" + Environment.NewLine + "https://github.com/" + repository, Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); }); break; } // =========================== // それ以外 // =========================== default: { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "引数を入力してください", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } } }
private static void Main(string[] args) { Console.WriteLine("Are you sure you want to continue? This will delete all your slack files [Y/N]", Color.Green); var key = Console.ReadLine(); if (key != "Y" && key != "N") { Console.WriteLine("Value does not match options. Your options are Y / N", Color.Red); Console.WriteLine("Press any key to exit...", Color.Red); Console.ReadKey(); return; } if (key == "N") { Console.WriteLine("You chose not to continue", Color.Red); Console.WriteLine("Press any key to exit...", Color.Red); Console.ReadKey(); return; } Console.WriteLine("Paste your slack api key please.", Color.Green); Console.WriteLine("Your api key can be obtained by requesting a token with the legacy token generator", Color.Yellow); Process.Start(LegacyTokenGeneratorUrl); var apiKey = Console.ReadLine(); if (string.IsNullOrEmpty(apiKey)) { Console.WriteLine("You didn't paste anything", Color.Red); Console.WriteLine("Press any key to exit... ", Color.Red); Console.ReadKey(); return; } var slackApi = SlackApi.Create(apiKey); var whoAmI = slackApi.Auth.Test(); if (!whoAmI.Ok) { Console.WriteLine("Api key you passed does not work", Color.Red); Console.WriteLine("Press any key to exit...", Color.Red); Console.ReadKey(); return; } Console.WriteLine("You are: {0}. Continue deleting your files? [Y/N]", whoAmI.User, Color.Green); key = Console.ReadLine(); if (key != "Y" && key != "N") { Console.WriteLine("Value does not match options. Your options are Y / N", Color.Red); Console.WriteLine("Press any key to exit...", Color.Red); Console.ReadKey(); return; } if (key == "N") { Console.WriteLine("You chose not to continue", Color.Red); Console.WriteLine("Press any key to exit... ", Color.Red); Console.ReadKey(); return; } var myfiles = slackApi.Files.List(new FilesListRequest { User = whoAmI.UserId, }); if (!myfiles.Ok) { Console.WriteLine("Could not access you files list", Color.Red); Console.WriteLine("Press any key to exit...", Color.Red); Console.ReadKey(); return; } if (myfiles.Files.Count == 0) { Console.WriteLine("You don't have any files", Color.Green); } else { Console.WriteLine("You have {0} files. Proceed deleting them", myfiles.Files.Count, Color.Yellow); var error = 0; foreach (var file in myfiles.Files) { Console.WriteLine("Deleting {0}", file.Name, Color.Green); try { slackApi.Files.Delete(file.Id); } catch { Console.WriteLine("Could not delete {0}", file.Name, Color.Red); error++; } } if (error == 0) { Console.WriteLine("All your files were erased", Color.Green); } else { var remainingFiles = slackApi.Files.List(new FilesListRequest { User = whoAmI.UserId, }); Console.WriteLine("Some files could not be removed:", Color.Red); if (remainingFiles.Ok) { foreach (var file in remainingFiles.Files) { Console.WriteLine(file.Name, Color.Red); } } } } Console.WriteLine("Press any key to exit...", Color.Yellow); Console.ReadKey(); }
public async Task Create(HttpRequestMessage request) { // =========================== // リクエストの取得・整形 // =========================== var data = await GetBody(request); // 引数の値 var method = data["text"]; // =========================== // 引数の値で処理を分ける // =========================== switch (method) { // =========================== // issue登録 // =========================== case "create": { // ============================= // GitHubアクセストークンの設定 // ============================= GitHubApi.SetCredential(data["user_id"]); // ============================= // 登録リポジトリ照会 // ============================= await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { List <string> labelNameList = null; // GitHub認証エラーハンドリング await AuthorizationExceptionHandler(data["channel_id"], data["response_url"], data["team_id"], async() => { // クライアントを用いてリポジトリ名からIssueのラベルを取得 var labelList = await GitHubApi.Client.Issue.Labels.GetAllForRepository(repository.Split('/')[0], repository.Split('/')[1]); // ラベル変数リストを文字列リストに変換 labelNameList = labelList.ToList().ConvertAll(x => x.Name); }); // =============================== // Issue作成用ダイアログモデル作成 // =============================== var model = CreateDialogModelForCreateIssue(data["trigger_id"], labelNameList); // ============================= // ダイアログAPI実行 // ============================= await SlackApi.ExecutePostApiAsJson(model, "https://slack.com/api/dialog.open", data["team_id"]); }); break; } case "export": { List <Issue> issueList = null; // ============================= // 登録リポジトリ照会 // ============================= await GetRepository(data["channel_id"], data["response_url"], data["team_id"], async repository => { // GitHub認証エラーハンドリング await AuthorizationExceptionHandler(data["channel_id"], data["response_url"], data["team_id"], async() => { var issueROList = await GitHubApi.Client.Issue.GetAllForRepository( repository.Split('/')[0], repository.Split('/')[1]); issueList = issueROList.ToList(); }); }); var issueText = string.Empty; foreach (var issue in issueList) { var assigneesString = issue.Assignees.Any() ? string.Join(" ", issue.Assignees.Select(x => x.Login)) : ""; issueText += $"```\n***issueID***\n{issue.Number}\n***アサイン者***\n{assigneesString}\n***タイトル***\n{issue.Title}\n***本文***\n{issue.Body}\n***登録者?***{issue.User.Login}\n***状態***\n{issue.State.StringValue}\n```"; } var model = new PostMessageModel() { Channel = data["channel_id"], Text = issueText, Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } // =========================== // それ以外 // =========================== default: { var model = new PostMessageModel() { Channel = data["channel_id"], Text = "引数を入力してください", Response_type = "ephemeral" }; await SlackApi.ExecutePostApiAsJson(model, data["response_url"], data["team_id"]); break; } } #endregion }
/// <summary> /// Initializes a new instance of the <see cref="SlackPageViewModel"/> class. /// </summary> public SlackPageViewModel() { Slack = new SlackApi(""); }