# Errbot 入门（2）

上一篇中，大致理解了一点 Errbot 的基础指令，这里尝试把 Errbot 接入到聊天室之中。

## Slack 接入

* 首先去 [Slack 网站](https://my.slack.com/services/new/bot) 注册一个机器人。
    
* 注册完成后，会得到一个 Token。
    

回到 Errbot 的配置文件，下面是一个样例。这里我们设置机器人接收 "@dustise" 这一 ID 的管理，`BOT_IDENTITY` 写入我们刚刚创建的 Bot 账号的 Token：

```python
import logging

BACKEND = 'Slack'

BOT_DATA_DIR = r'/errbot/data'
BOT_EXTRA_PLUGIN_DIR = r'/errbot/plugins'

BOT_LOG_FILE = r'/errbot/errbot.log'
BOT_LOG_LEVEL = logging.WARNING

BOT_ADMINS = ('@dustise', )  # !! Don't leave that to "@CHANGE_ME" if you connect your errbot to a chat system !!

BOT_IDENTITY = {
    'token': '......',
}
```

接下来启动机器人，打开 Slack，进入频道，随意`@newbot`邀请其加入频道，输入`!help`，就能看到机器人输出的帮助指令了。输入`!tryme`指令，会看到机器人的回复。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745895519939/7cc12f0d-d997-447b-a065-543bd02a4f90.png align="center")

如果使用的是 Errbot 指定的管理账号，可通过私聊的方式为其发送`!restart`等管理指令。

在聊天室中输入`!whoami`指令，会看到 Errbot 对当前用户的识别情况。

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1745895526285/86866355-ed01-4eb2-ab39-68e2c17f559e.png align="center")

## `@` 指令

`!` 指令一般用于固定格式的命令，而 `@` 也就是 "Mention" 就随意的多了，Errbot 同样提供了这种支持。

在插件的`.py`文件中加入如下方法：

```python
    def callback_mention(self, message, mentioned_people):
        if self.bot_identifier in mentioned_people:
            self.send(message.frm, 'Errbot has been mentioned !')
```

在私聊窗口输入 `!restart` 重新载入 Errbot 之后，在房间中输入 `@newbot hello`，会看到 Errbot 在私聊窗口中进行了回复。
