Changing Slack bot name / image using Slash Commands? - python

I'm setting up a slackbot with slash commands and according to API you're able to pass username to set the username of a bot. When I try it, the name does not update. Does this not work with slash commands in the same way that it does with bots?
from bottle import run, post, request
#post('/hello')
def hello():
mydict = request.forms.get('text')
response = {
"username": "test",
"response_type": "in_channel",
"text": "Greetings!"
}
return response
matt [11:00 AM]
/hello
MyBot APP [11:00 AM]
Greetings!

Correct. You can not change the name of the app that appears on Slack when you respond to a request like that. It will always show the name you configured in the app settings.
However, there is a workaround for your use case: Instead of replying to the received request directly you can send a message into the channel / to the user with chat.postMessage, which will allow you to specify a custom username and icon.

Related

redirect uri mismatch in fastapi

I hope everyone is fine. I am trying to implement google sso on my fastapi app. after entering the user credentials is entered and it gets redirected while redirecting i am getting this error
google_sso = GoogleSSO("client-id", "client-secret", "http://127.0.0.1:8000/google/callback/")
#app1.get("/google/login")
async def google_login():
"""Generate login url and redirect"""
return await google_sso.get_login_redirect()
#app1.get("/google/callback")
async def google_callback(request: Request):
"""Process login response from Google and return user info"""
user = await google_sso.verify_and_process(request)
print("Hellooooooooooooooo")
print(user, "11111111111111")
return {
"id": user.id,
"picture": user.picture,
"display_name": user.display_name,
"email": user.email,
"provider": user.provider,
}
I have shared the URL configuration in google dashboard in below screenshot
enter image description here
the error i have mentioned below
oauthlib.oauth2.rfc6749.errors.CustomOAuth2Error: (redirect_uri_mismatch) Bad Request
The problem could lay in the process_login() function which is getting called in the verify_and_process() function in your /callback api.
Let's take a look inside the process_login() function (https://tomasvotava.github.io/fastapi-sso/sso/base.html#fastapi_sso.sso.base.SSOBase.verify_and_process):
async def process_login(self, code: str, request: Request) -> Optional[OpenID]:
"""This method should be called from callback endpoint to verify the user and request user info endpoint.
This is low level, you should use {verify_and_process} instead.
"""
url = request.url
current_url = str(url).replace("http://", "https://")
current_path = f"https://{url.netloc}{url.path}"
I guess the (redirect_uri_mismatch) error is because you are using a HTTP redirect_url in your GoogleSSO() call:
google_sso = GoogleSSO("client-id", "client-secret", "http://127.0.0.1:8000/google/callback/")
Inside the process_login() function the HTTP of the redirect url inside the url of your request is replaced with HTTPS:
url = request.url
current_url = str(url).replace("http://", "https://")
After that replacement you have a mismatch with your redirect url, because
https://127.0.0.1:8000/google/callback/
is not
http://127.0.0.1:8000/google/callback/
They are two different urls.
Solution could be that you secure your server with HTTPS via self signed certificates.
(That one is very simple: https://dev.to/rajshirolkar/fastapi-over-https-for-development-on-windows-2p7d)
Btw. did you register you app in the google cloud (https://developers.google.com/identity/sign-in/web/sign-in)? Because you are using "client-id" and "client-secret" as parameters.
try it use 127.0.0.1:8000/google/callback #remove /
or
fix url #app1.get("/google/callback/") #add /
This is because the port number is changing in the redirect URI, everytime you run the application.
So everytime you run it it becomes:
http://localhost:65280/
http://localhost:65230/
http://localhost:63280/
And so forth. I dont have a solution for you yet. Working on it myself right now.

trying to create a Discord webhook, with the avatar and username assigned inside the code

I am trying to create a Discord webhook, with the avatar and username assigned inside the code.
This is the code I have so far. I have no idea what comes before or after this.
{
"username": "Webhook",
"avatar_url": "https://i.imgur.com/4M34hi2.png",
"content": "Text message. Up to 2000 characters.",
}
Can you instead create the webhook from Discord's GUI (see "Making A Webhook" section)?
If you need to do it programmatically, can you confirm you're sending a POST request to /channels/{channel.id}/webhooks and have the MANAGE_WEBHOOKS role, per the webhook resource? That doc also only shows name and avatar as fields for a create webhook request.

How to add custom username in Slack API files.upload using bot user?

I am uploading a file using files.upload method and want to display username like XYZ or any other custom username (which works in chat.postMessage).
Below code is working, file is uploading but in response username returns empty and in Slack message displays with app name Demo App.
response = slack.api_call(
'files.upload',
channels='#website',
file=io.BytesIO(file.read()),
title='File from slack api',
initial_comment='Create by XYZ',
username='XYZ',
)
When posting text message it is working fine and displaying same name I mention in username
response = slack.api_call(
'chat.postMessage',
channel='#website',
text='This is from slack demo',
username='ABC'
)
This is not a feature that the this Slack API method offers. If you share a file is will always be showing the name of the actual token owner who uploaded it. Which may be a bot user or real user.

administrator has not consented to use the application -- Azure AD

I am trying to obtain a token from Azure AD from Python client application. I want users to seamlessly authenticate with just a username and password (client_id / secret will be embedded in the app). I registered my app and given it all permissions and hit the "grant permissions" button in the new portal according to this post:
The user or administrator has not consented to use the application - Send an interactive authorization request for this user and resource
I am sending an http post to:
https://login.microsoftonline.com/{tenant_id}/oauth2/token
with the following data:
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
body = "resource={0}&grant_type=password&username={1}&password={2}&client_id={3}&client_secret={4}&scope=openid".format(app_id_uri,user,password,client_id,client_secret)
I cannot seem to get past this error no matter what I try:
b'{"error":"invalid_grant","error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID \'078c1175-e384-4ac7-9116-efbebda7ccc2\'. Send an interactive authorization request for this user and resource.
Again, my goal:
User enters user / pass and nothing else. App sends user / pass / client_id / client_secret, obtains token.
According to your comment:
The message I'm receiving says to do an interactive request but that is exactly what I'm trying to avoid because this is a python app with no web browser and I'm trying to avoid complexity.
I think you want to build a daemon app or an app only application integrating with Azure AD. You can refer to https://graph.microsoft.io/en-us/docs/authorization/app_only for the general introduction.
Furthermore, you can leverage the ADAL for Python to implement this functionality with a ease. Also, you can refer to client_credentials_sample.py for a quick start.
You should try logging in as an admin to be able to give consent to use the application on your tenant at all.

Slack API: Do Something when button is clicked

I am using Python and it's Slacker API to post messages to a slack channel and it's posting the messages nicely.
Now what I want to do is create a button that says, More Info and when it's clicked, I want to show a list of items.
But when the button is clicked, slackbot says oh no, something weng wrong, Please try that again
Here is an example: link
Below is my json and the code
msg = "<!here> Hello guys! "
moreInfo = ['person', 'person2', 'person3']
message = [{
"title": "Lunch time has been decided",
"text": "You will also be joining",
"actions": [
{
"name": "buttonName",
"text": "More Info",
"type": "button",
"value": moreInfo
}]
}]
slack.chat.post_message('#teamChannel', msg, username='my_username', attachments=message)
And this is the what it looks like in Slack when I click on More info button.
Any help is appreciated! Thanks :)
Do you have a button endpoint setup already? If not, you will see that error message.
Or if you made the same mistake I did, you are using the incorrect token. This is non-obvious from the Slack docs. You can post a message with a custom integration bot token, including using attachments (i.e. interactive buttons). However, if you want to actually respond to a button press, you need to post the message with a full-fledged Slack app token (even if you don't intend on releasing your app into the wild). That means creating an endpoint for your oauth2 flow to add your app to a Slack team, and acquiring your bot token there.
Create a Slack app if you haven't already.
Create an oauth GET endpoint (e.g. /slack/authorize)
Set your Slack app's "Redirect URI" to your auth endpoint
Your endpoint should expect a code in its query parameters. Call the Slack API oauth.access with your app's client_id, client_secret, and that code to exchange for aforementioned app token. To post your interactive message from a bot, you will need the bot token (prefixed "xoxo-").

Categories

Resources