scrapinghub / slackbot

A chat bot for Slack (https://slack.com).
MIT License
1.26k stars 394 forks source link

Reply in thread #136

Closed pshriwise closed 7 years ago

pshriwise commented 7 years ago

Is there a way to have the bot reply in a thread? Right now, our bot successfully listens to messages for threads in a channel, but it replies to the channel.

I'm aware that threads are a relatively new feature, but is there a way to reply within the thread? If not, will this be possible in the future?

NiekKeijzer commented 7 years ago

I have been playing with this feature this morning and it seems fairly easy to implement by including the thread_id in the message send to the API.

The question remains how Slackbot should be made aware that the message should be send to a thread. I would suggest something along the lines of the code below. Notice the extra in_thread argument passed to reply method.

@listen_to("Howdy")
def greet(message):
    message.reply("Howdy to as well", in_thread=True)

Internally Slackbot would determine the thread's ID by looking at the message.body property. For example the following property could be added to the message class:

@property
def thread_ts(self):
    try:
        thread = self.body['thread_ts']
    except KeyError:
        # This would mean there's no thread yet and one will be started
        thread = self.body['ts']

    return thread

If people are interested, I think I could implement this one of these days.

janik6n commented 7 years ago

This would be highly appreciated :)

jcleal commented 7 years ago

Just wondering if this enhancement is planned sometime soon :)

NiekKeijzer commented 7 years ago

I must be honest, I kind of slipped my mind. I will see if I can make a decent start on it this weekend.

NiekKeijzer commented 7 years ago

I created a pull request to add this functionality #153

jtatum commented 7 years ago

Thanks a lot, @NiekKeijzer 😄

forgettableEgg commented 7 years ago

I was just trying the in_thread parameter using the code you provided for testing and i keep getting this error. Any idea why? Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/slackbot/dispatcher.py", line 55, in _dispatch_msg_handler func(Message(self._client, msg), *args) File "/home/dev/Desktop/Github/securitybot/securitybot2/securitybot2/plugins.py", line 27, in help message.reply("Here's a threaded reply", in_thread=True) File "/usr/local/lib/python2.7/dist-packages/slackbot/dispatcher.py", line 175, in wrapped return func(self, text, *a, **kw) TypeError: reply() got an unexpected keyword argument 'in_thread'

jtatum commented 7 years ago

Hi, @jaysig112 - are you using the latest code on the develop branch? You may want to rerun setup or pip install requirements.txt - the threaded reply option requires a relatively new version of one of our library dependencies (slacker>=0.9.50).

forgettableEgg commented 7 years ago

I just installed everything today and I checked slacker it is version 0.9.50

jtatum commented 7 years ago

What version of slackbot do you have? The version with in_thread is not yet on pypi. You'll need to install slackbot from the develop branch of git.

forgettableEgg commented 7 years ago

Yep that fixed it! Thanks! While I have you here if I wanted to have it keep replying in the thread how would I go about that. Like I start a conversation with the bot it replies in the thread with a question then I answer in the thread and it keeps going until I answer all the bots questions.

Jimsocks commented 4 years ago

How do I use this feature?