Created
June 15, 2021 10:45
-
-
Save andrewfulton/f6ad7535393a1ca508808b30868b3abf to your computer and use it in GitHub Desktop.
Slack API utility function(s)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| const slaxios = axios.create({ | |
| baseURL: 'https://slack.com/api/', | |
| headers: { | |
| Authorization: `Bearer ${process.env.SLACK_TOKEN}`, | |
| }, | |
| }); | |
| type EmojiListSlackResponse = { | |
| ok: boolean; | |
| emoji: Record<string, string>; | |
| }; | |
| export type EmojiList = { | |
| name: string; | |
| url: string; | |
| }[]; | |
| const resolveAlias = ( | |
| alias: string, | |
| emojiMap: Record<string, string> | |
| ): string => emojiMap[alias.replace('alias:', '')] ?? ''; | |
| export const getEmoji = async (): Promise<EmojiList> => { | |
| try { | |
| const emojiData = await slaxios.get<EmojiListSlackResponse>('emoji.list'); | |
| if (!emojiData.data.ok) { | |
| throw new Error('Slack API error'); | |
| } | |
| // Turn the Record/Dictionary that Slack gives use into an array, | |
| // resolving any aliases along the way. | |
| return Object.entries(emojiData.data.emoji) | |
| .map(([name, url]) => ({ | |
| name, | |
| url: | |
| url.indexOf('alias:') === 0 | |
| ? resolveAlias(url, emojiData.data.emoji) | |
| : url, | |
| })) | |
| .filter((item) => item.url !== ''); | |
| } catch (ex) { | |
| return []; | |
| } | |
| }; | |
| export const post = async () => {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment