Last active
April 24, 2019 01:51
-
-
Save totuworld/19591e2cd9fbdaffb55b7370e6611760 to your computer and use it in GitHub Desktop.
slack-bot-tutorial-code
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
// 아래 코드는 공식 문서에서 발췌 한 후 아주 작은 수정을 했다. | |
const { RTMClient } = require('@slack/rtm-api'); | |
const token = process.env.SLACK_BOT_TOKEN; | |
const rtm = new RTMClient(token); | |
// Listen for users who join a channel that the bot user is a member of | |
// See: https://api.slack.com/events/member_joined_channel | |
rtm.on('member_joined_channel', async event => { | |
try { | |
// Send a welcome message to the same channel where the new member just joined, | |
// and mention the user. | |
const reply = await rtm.sendMessage( | |
`Welcome to the channel, <@${event.user}>`, | |
event.channel | |
); | |
console.log('Message sent successfully', reply.ts); | |
} catch (error) { | |
console.log('An error occurred', error); | |
} | |
}); | |
(async () => { | |
await rtm.start(); | |
})(); |
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 * as debug from 'debug'; | |
import { | |
LogLevel, | |
RTMClient, | |
WebClient, | |
WebAPICallResult | |
} from '@slack/client'; | |
const log = debug('sb:server'); | |
const SLACK_TOKEN: string = process.env.SLACK_TOKEN || 'token'; | |
const rtm = new RTMClient(SLACK_TOKEN, { logLevel: LogLevel.ERROR }); | |
const webClient = new WebClient(SLACK_TOKEN); | |
/* | |
* rtm.on 으로 사용할 수 있는 event type은 다음 문서를 참고한다. | |
* https://api.slack.com/events | |
*/ | |
rtm.on('member_joined_channel', async event => { | |
log(event); | |
try { | |
const welcomeMsg: WebAPICallResult & { | |
ts?: string; | |
} = await webClient.chat.postMessage({ | |
token: SLACK_TOKEN, | |
channel: event.channel, | |
text: `<@${event.user}> 님, 환영합니다.` | |
}); | |
log(welcomeMsg); | |
} catch (error) { | |
log('An error occurred', error); | |
} | |
}); | |
(async () => { | |
const start = await rtm.start(); | |
log('start: ', start); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment