I have a bot, and I can interact with it. And there's another bot, and I would like my bot to chat with that bot, when they are in the same channel. Is that even possible?
I tried to include a mention like <@IDBOT|name>: text, and even though it appears to me that the mention was successful, the other bot doesn't respond. If I post this mention it will work.
Is there a limitation here?
Yes, it can.
I had the same problem, it turns out that I had included some code that I didn't understand, and that code was preventing the response. I e-mailed slack about it and they set me straight.
The problematic code was this:
if event["type"] == "message" and not "subtype" in event:
user_id, message = parse_direct_mention(event["text"])
if user_id == self_id:
return message, event["channel"]
The helpful response from slack:
The condition below is what's preventing your bot from listening to bot's messages:
if event["type"] == "message" and not "subtype" in event:When a message is sent by a bot, it will have a subtype, so this means that your logic is disregarding any bot message. This is helpful because it prevents your bot from responding to its own messages, which would create an infinite loop.
You'll need to modify this condition so that your bot still "ignores" its own messages, but processes messages from other bots. You could do this for instance by looking at the bot ID or user ID and discarding those messages, but not messages from other bots.
In my case, I want the bot to respond to humans always, and bots only if they are trusted, so I did this:
from_user = "subtype" not in event
from_friend_bot = (event["subtype"] == "bot_message") and (event['username'] == f'{ping_source}')
if from_user or from_friend_bot:
user_id, message = parse_direct_mention(event["text"])
if user_id == self_id:
return message, event["channel"]
Yes, bots can talk to each other in a channel.
It depends on how you control the listening bot. I'm using a fork of the official Python Slackbot code (https://github.com/bscan/python-slackbot) and in it, I check for <@U1234567> where U1234567 is the user_id of the bot. When you mention @mybot, Slack replaces @mybot with <@U1234567> in the message. However, when posting as a bot, Slack doesn't replace a callout with the user_id. Instead, a bot can directly put <@U1234567> in the message (and post using as_user=True). Slack will display <@U1234567> as @mybot in the channel and the bot will be able to detect it if looking for that exact message string.
Source: played around until bots talked to each other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With