-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Direct message when command run in public channel #4
Comments
Hello David, I am glad you like it. If you want the bot to respond to a user by direct message from a command typed in a public channel, you have to get the DM channel ID of the user from the context. For example : protected function execute($message, $context) {
if (isset($context['ims'])) {
foreach ($context['ims'] as $im) {
if ($im['user'] == $this->getCurrentUser()) {
$this->send($im['id'], null, 'My message');
}
}
}
} Note that a DM channel must be opened between the user and the bot. I just added a method getImIdFromUserId which does the same. protected function execute($message, $context) {
$this->send($this->getImIdFromUserId($this->getCurrentUser()), null, 'My message');
} Please let me know if it works for you |
Hi !
I understand. What I would like to achieve is to open the channel between the user and the bot when a command is issued on the public channel. I managed to do it using the im.open method (https://api.slack.com/methods/im.open) but it is quite messy because I'm doint it from outside of the bot code. I would love for this to be integrated in the main code so that I could simply open the channel and send a message to it within a command (if that makes sense). |
Unfortunately the project is not complete and lacks the support for API methods like im.open. |
I see. I would not dare to open a pull request because in my opinion, the way I implemented the feature is dirty. Anyways, here is what I have done (it might be useful to someone passing by, or you might want to implement it correctly if you have too much free time 😃) : I created a SlackAPI.php file in src subdirectory : abstract class SlackAPI {
public function getIMChannel($userId) {
$client = new \GuzzleHttp\Client();
$url = 'https://slack.com/api/im.open?';
$url.= 'token='.'xoxb-41213195428-Zs1GnUPoj1M3fqE18yJl39T2';
$url.= '&user='.$userId;
$res = $client->request('GET', $url);
$obj = json_decode($res->getBody(), true);
if (isset($obj['channel']['id'])) {
return $obj['channel']['id'];
}
else {
return "";
}
}
} Token is hardcoded as you can see, also there is surely a better way to handle the value returned (direct message channel id or empty string), etc. I then reference this file in the main script : require 'src/SlackAPI.php'; Finally I open a direct message channel like this in a bot command : $imchannel = \SlackAPI::getIMChannel($this->getCurrentUser());
$this->send($imchannel, null, "Hello !"); I use the guzzlehttp/guzzle package so it should be required :
Hopefully someone will make this better. |
Hi,
I've been playing around with php-slack-bot and I like it so far. However, I have a question I would like to ask : is there any way to respond to a user running a command in a public channel by direct message ?
Thanks,
David
The text was updated successfully, but these errors were encountered: