I'm using python3.6 and trying to send message to myself to start interacting in slack.
I've installed pip install slackclient and am using slackclient v2.2.1
channel ID is extracted from slack link of my account https://XXXXX.slack.com/messages/XXXXXXXXX
I would like to see Hi message in my slack account. any suggestions.
client = slack.WebClient("BOT_USER_TOKEN", timeout=30)
client.chat_postMessage(
channel='CHANNEL_ID',
text='Hi!')```
The bot user token is linked to a bot user that is created with your app. If you use the bot token you will only have access to channels that this bot user is a member of.
So to make your script work you need to do one of the following:
Use the access token instead of the bot token (that one is linked to the user that installed the Slack app)
Invite the bot user to the channel you are trying to send a message to
As you want to start with basics, I would recommend to use public channels first, which will always work.
Direct messages are a little bit more complicated. To send a direct message to a user (e.g. from your bot user to yourself) you need to first open a direct message channel with conversations.open, which will give you a new channel ID. And then use that channel ID for sending a message.
Related
My telegram bot receives messages sent by user to my bot in private chats but not receives messages sent by users in group chats. Any options/api for getting group chat messages also,.
Talk to #botfather and disable the privacy mode.
Sequence within a BotFather chat:
You: /setprivacy
BotFather: Choose a bot to change group messages settings.
You: #your_name_bot
BotFather: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
'Disable' - your bot will receive all messages that people send to groups.
Current status is: ENABLED
You: Disable
BotFather: Success! The new status is: DISABLED. /help
By default A Bot will receive only messages addressed to it by any user directly via posting by /command#YourBot any message you send.
After that it vill be available via getUpdates API call.
In browser it will be:
https://api.telegram.org/botToken/getupdates
Find the related message in output JSON and grab chatId. It will allow you to answer back with:
https://api.telegram.org/botToken/sendmessage?chat_id=123456788&text=My Answer
Make your bot by admin in group.
You can access all avaliable settings from all of your bots by sending /mybots to Botfather. Choose the bot, then Bot Settings and Group Privacy. If its disable (default), you can tap on Turn off.
Now its possible to receive the chat history using GetUpdates. This can be done via HTTP API or the frameworks. For example, in C# (.NET Core) like this:
var bot = new TelegramBotClient(ApiToken);
var updates = bot.GetUpdatesAsync().Result;
foreach(var update in updates) {
Console.WriteLine($"{update.ChannelPost.Date} {update.ChannelPost.Text}");
}
But keep in mind that this feature has some kind of perfect forward secrecy implemented. So you only get messages that were send after group privacy is disabled. As a result, the GetUpdates result is empty until some post was made.
if you added your bot before disable the privacy mode, you should remove the bot from the group and add it again
I created a small test bot connecting a local python script with Slack. The main task of the bot is to create a custom string, and send it to a channel in Slack. This is done with
from slack_sdk import WebClient
client = WebClient(token = slack_token)
slack_message = "Hello #demo_user" # #demo_user is an existing user, which can be mentioned in chat
client.chat_postMessage(channel = channel_ID, text = slack_message)
In this message I would like to explicitly mention certain groups or persons by using #<username>. This works fine if I type out the message directly in Slack, but if the bot itself sends the message, the groups are not messaged/pinged, even though the message contains the corresponding handle. What is happening here, and how can I ping those people/groups instead via Python?
I'm using the Slack API with NodeJS, but it might come from the link_names argument. Did you try to set it to true?
https://api.slack.com/methods/chat.postMessage#arg_link_names
I am building a desktop application, and I need to send a message through it, that would be posted from the logged user's discord account. is there any way to do this? I know about webhooks and on_message bot-event.
You can't do this in discord.py, but you can in older versions of the nodejs version by simply passing the user's token instead of the bot token
I'm trying to get the slack UserId of the user who initiated a conversation with a bot.
I tried using users.identity scope and api, but I'm only getting the UserId of the user who created the bot. The bot will be used by multiple users so I want to have an isolated chat for each user by getting the exact UserId of the user who is currently having a conversation with the bot.
I'm using Python, Amazon Lex and Slack.
Can somebody point me to the right path?
If the conversation is initiated as a direct message with the bot, you can subscribe to message.im event. You'll receive this event every time a user sends a message to the bot. In the response JSON body, event.user should provide you the userId.
It could be a different solution depending upon how you are making the app, and where do you need this information.
This is a smaller piece of a larger program, but to zero in on this specific issue, I've been having quite a bit of trouble recently setting up a program in python using the slackclient module that can invite a user to a private channel (called groups in the API). The program has 2 tokens, one for the bot and one for the actual app.
My issue is that while I can invite the bot to the private channel it should be inviting people to, bots can't have the required scopes to do so. If I switch over to the app token which does have those scopes, it can't complete the operation because apps can't be invited to a channel.
The documentations for this API Call can be found here.
This is the invite command I'm using:
SlackClient(slack_token).api_call("groups.invite", channel='GXXXXXXXX', user='UXXXXXXXX')
While the bot can access the channel information, attempting to invite someone with the bot returns this error:
'error': 'missing_scope', 'provided': 'identify,bot:basic', 'ok': False, 'needed': 'groups:write'
Switching to the app token returns this error:
'error': 'channel_not_found', 'ok': False
Am I missing something here? Or is it intentional that apps not be allowed to invite users to private channels at all?
The reason this does not work is as follows:
Using the bot token you get your first error. groups.invite does not work with the bot token in general. so you need to use the user token for this API method.
When using the user token you get the 2nd error, because the user that installed your Slack app is apparently not a member of that private group. So he can not invite another user to it