check-communicable.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 checkSlackScope = (result: any) => {
  39. const slackScope = result.response_metadata.scopes;
  40. if (!slackScope.includes('commands', 'team:read', 'chat:write')) {
  41. throw new Error('Scope error');
  42. }
  43. };
  44. /**
  45. * Retrieve Slack workspace name
  46. * @param client
  47. */
  48. const retrieveWorkspaceName = async(client: WebClient): Promise<string> => {
  49. const result = await client.team.info();
  50. if (!result.ok) {
  51. throw new Error(result.error);
  52. }
  53. return (result as any).team?.name;
  54. };
  55. /**
  56. * Get token string to ConnectionStatus map
  57. * @param tokens Array of bot OAuth token
  58. * @returns
  59. */
  60. export const getConnectionStatuses = async(tokens: string[]): Promise<{[key: string]: ConnectionStatus}> => {
  61. const map = tokens
  62. .reduce<Promise<Map<string, ConnectionStatus>>>(
  63. async(acc, token) => {
  64. const client = generateWebClient(token);
  65. const status: ConnectionStatus = {};
  66. try {
  67. // try to connect
  68. const res = await testSlackApiServer(client);
  69. await checkSlackScope(res);
  70. // retrieve workspace name
  71. status.workspaceName = await retrieveWorkspaceName(client);
  72. }
  73. catch (err) {
  74. status.error = err;
  75. }
  76. (await acc).set(token, status);
  77. return acc;
  78. },
  79. // define initial accumulator
  80. Promise.resolve(new Map<string, ConnectionStatus>()),
  81. );
  82. // convert to object
  83. return Object.fromEntries(await map);
  84. };
  85. /**
  86. * @param token bot OAuth token
  87. * @returns
  88. */
  89. export const testToSlack = async(token:string): Promise<void> => {
  90. const client = generateWebClient(token);
  91. const res = await testSlackApiServer(client);
  92. await checkSlackScope(res);
  93. };