@zhin.js/adapter-slack
v1.0.2
Published
zhin adapter for slack
Readme
@zhin.js/adapter-slack
Slack adapter for zhin.js framework.
Installation
pnpm add @zhin.js/adapter-slackConfiguration
Socket Mode (Recommended for Development)
import { defineConfig } from 'zhin.js'
export default defineConfig({
bots: [
{
name: 'my-slack-bot',
context: 'slack',
token: 'xoxb-your-bot-token',
signingSecret: 'your-signing-secret',
appToken: 'xapp-your-app-token',
socketMode: true
}
]
})HTTP Mode (For Production with Public URL)
import { defineConfig } from 'zhin.js'
export default defineConfig({
bots: [
{
name: 'my-slack-bot',
context: 'slack',
token: 'xoxb-your-bot-token',
signingSecret: 'your-signing-secret',
socketMode: false,
port: 3000
}
]
})Features
- ✅ Send and receive text messages
- ✅ Support for rich media (images, files)
- ✅ Message formatting (Slack mrkdwn)
- ✅ Reply to messages and threads
- ✅ Mentions (@user, #channel)
- ✅ Links and attachments
- ✅ Socket Mode and HTTP mode
- ✅ Private messages and channels
Setting Up Your Slack App
- Go to Slack API
- Create a new app
- Add Bot Token Scopes:
chat:write- Send messageschat:write.public- Send messages to public channelschannels:read- View basic channel infochannels:history- View messages in channelsgroups:read- View basic private channel infogroups:history- View messages in private channelsim:read- View basic direct message infoim:history- View messages in direct messagesmpim:read- View basic group direct message infompim:history- View messages in group direct messagesusers:read- View user infofiles:read- View filesfiles:write- Upload files
- Enable Socket Mode (if using Socket Mode):
- Go to Socket Mode settings
- Enable Socket Mode
- Generate an app-level token with
connections:writescope
- Subscribe to events:
message.channels- Messages in public channelsmessage.groups- Messages in private channelsmessage.im- Direct messagesmessage.mpim- Group direct messagesapp_mention- When the bot is mentioned
- Install the app to your workspace
- Copy the Bot User OAuth Token (
xoxb-...) - Copy the Signing Secret
- Copy the App-Level Token (
xapp-...) if using Socket Mode
Usage Examples
Basic Message Handling
import { addCommand, MessageCommand } from 'zhin.js'
addCommand(new MessageCommand('hello')
.action(async (message) => {
return 'Hello from Slack!'
})
)Send Rich Messages
addCommand(new MessageCommand('info')
.action(async (message) => {
return [
{ type: 'text', data: { text: '*Bold* and _italic_ text\n' } },
{ type: 'link', data: { url: 'https://slack.com', text: 'Visit Slack' } }
]
})
)Mention Users
addCommand(new MessageCommand('mention <userId:text>')
.action(async (message, result) => {
return [
{ type: 'at', data: { id: result.params.userId } },
{ type: 'text', data: { text: ' Hello!' } }
]
})
)Reply in Thread
addCommand(new MessageCommand('thread')
.action(async (message) => {
// Reply in a thread by passing the message timestamp
await message.$reply('This is a threaded reply!', true)
})
)Slack-Specific Features
Slack Formatting
Slack uses mrkdwn format:
*bold*for bold_italic_for italic~strike~for ~~strikethrough~~`code`forcode> quotefor blockquotes
User and Channel Mentions
The adapter automatically converts:
<@U12345678>to user mentions<#C12345678>to channel mentions
File Uploads
Upload files using the file segment type with a local file path.
Limitations
- Message recall requires channel information (not available from message ID alone)
- Some Slack features like interactive components require additional setup
License
MIT
