check-communicable.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import axios, { AxiosError } from 'axios';
  2. import { WebClient } from '@slack/web-api';
  3. import { generateWebClient } from './webclient-factory';
  4. import { ConnectionStatus } from '../interfaces/connection-status';
  5. /**
  6. * Check whether the HTTP server responds or not.
  7. *
  8. * @param serverUri Server URI to connect
  9. * @returns AxiosError when error is occured
  10. */
  11. export const connectToHttpServer = async(serverUri: string): Promise<void|AxiosError> => {
  12. try {
  13. await axios.get(serverUri, { maxRedirects: 0, timeout: 3000 });
  14. }
  15. catch (err) {
  16. return err as AxiosError;
  17. }
  18. };
  19. /**
  20. * Check whether the Slack API server responds or not.
  21. *
  22. * @returns AxiosError when error is occured
  23. */
  24. export const connectToSlackApiServer = async(): Promise<void|AxiosError> => {
  25. return connectToHttpServer('https://slack.com/api/');
  26. };
  27. /**
  28. * Test Slack API
  29. * @param client
  30. */
  31. const testSlackApiServer = async(client: WebClient): Promise<any> => {
  32. const result = await client.api.test();
  33. if (!result.ok) {
  34. throw new Error(result.error);
  35. }
  36. return result;
  37. };
  38. const checkSlackScopes = (result: any) => {
  39. const slackScopes = result.response_metadata.scopes;
  40. const correctScopes = ['commands', 'team:read', 'chat:write'];
  41. if (correctScopes.every(e => slackScopes.includes(e))) {
  42. return;
  43. }
  44. throw new Error('Scope error');
  45. };
  46. /**
  47. * Retrieve Slack workspace name
  48. * @param client
  49. */
  50. const retrieveWorkspaceName = async(client: WebClient): Promise<string> => {
  51. const result = await client.team.info();
  52. if (!result.ok) {
  53. throw new Error(result.error);
  54. }
  55. return (result as any).team?.name;
  56. };
  57. /**
  58. * Get token string to ConnectionStatus map
  59. * @param tokens Array of bot OAuth token
  60. * @returns
  61. */
  62. export const getConnectionStatuses = async(tokens: string[]): Promise<{[key: string]: ConnectionStatus}> => {
  63. const map = tokens
  64. .reduce<Promise<Map<string, ConnectionStatus>>>(
  65. async(acc, token) => {
  66. const client = generateWebClient(token);
  67. const status: ConnectionStatus = {};
  68. try {
  69. // try to connect
  70. await testSlackApiServer(client);
  71. // retrieve workspace name
  72. status.workspaceName = await retrieveWorkspaceName(client);
  73. }
  74. catch (err) {
  75. status.error = err;
  76. }
  77. (await acc).set(token, status);
  78. return acc;
  79. },
  80. // define initial accumulator
  81. Promise.resolve(new Map<string, ConnectionStatus>()),
  82. );
  83. // convert to object
  84. return Object.fromEntries(await map);
  85. };
  86. /**
  87. * @param token bot OAuth token
  88. * @returns
  89. */
  90. export const testToSlack = async(token:string): Promise<void> => {
  91. const client = generateWebClient(token);
  92. const res = await testSlackApiServer(client);
  93. await checkSlackScopes(res);
  94. };