AJwr
AJwr

Reputation: 618

Custom Slack Bot Not Posting as User

I am currently trying to integrate a custom Slack bot into one of my channels. However, I am having an issue where the bot is posting not as the custom bot user but rather as me.

Image of bot test

The bot responds to my custom commands and stuff but for some reason is not posting as the bot that I set up. I am using the API token that was given to me when setting up the bot and added it to the channel I am currently testing in. Anyone know what might be causing this issue?

Relevant code:

def handle_command(command, channel):
    """Receives commands directed at the bot and determines if they are valid commands. If so,
    then acts on the commands. If not, returns back what it needs for clarification.
    """
    response = "Hello there!"
    if command.startswith("yes"):
        response = "You posted 'yes'!"
    SLACK_CLIENT.api_call("chat.postMessage", channel=channel,
                          text=response, as_user=True)

def parse_slack_output(slack_rtm_output):
    """The Slack Real Time Messaging API is an events firehose. This parsing function returns
    None unless a message is directed at the Bot, based on its ID.
    """
    output_list = slack_rtm_output
    if output_list and len(output_list) > 0:
        for output in output_list:
            if output and 'text' in output and AT_BOT in output['text']:
                # return text after the @ mention, whitespace removed
                return output['text'].split(AT_BOT)[1].strip().lower(), \
                       output['channel']
    return None, None


def main():
    """Obtains Google Credentials to rotate and update a Google Spreadsheet that keeps track of the
    current engineer with 10 percent time. Notifies the engeineering team through a Google Calendar
    event.
    """

    if SLACK_CLIENT.rtm_connect():
        print "Bot connected and running."
        while True:
            command, channel = parse_slack_output(SLACK_CLIENT.rtm_read())
            if command and channel:
                handle_command(command, channel)
            time.sleep(1)
    else:
        print "Connection failed."

SLACK_CLIENT was initialized using the API and the given token, and AT_BOT is just a constant for the '@' character and my bot's name.

Upvotes: 2

Views: 2390

Answers (3)

Ben
Ben

Reputation: 308

Passing as_user="true" rather than as_user=True worked for me. I don't think the client does any special handling / conversion of Python booleans and API probably expects "true" as indicated in the docs.

as_user true Optional
Pass true to post the message as the authed user, instead of as a bot. Defaults to false. See authorship below. This argument may not be used with newer bot tokens.

from https://api.slack.com/methods/chat.postMessage

Upvotes: 0

Erik Kalkoken
Erik Kalkoken

Reputation: 32737

Another potential cause for this behavior could be which token is used.

When you install a Slack app with a bot user you will always get two access tokens: A user access token and a bot access token. (source)

If you use the bot access token messages will always show up with the bot name, regardless of the as_user setting for chat.postMessage. (source)

Upvotes: 1

Karim Tabet
Karim Tabet

Reputation: 1847

I think it may be because you are setting as_user=True.

The official documentation says

Pass true to post the message as the authed user, instead of as a bot.

Have you tried setting it to false?

SLACK_CLIENT.api_call("chat.postMessage", channel=channel,
                      text=response, as_user=False)

Upvotes: 1

Related Questions