Unable to send a message as the user who logged in through sign in with slack integrated in my django project?

I am trying to integrate slack messaging in django project so for that I had integrated sign in with slack option in my project and created a new slack app and I had added chat:write and users:read scopes to my User Token Scopes of my slack app and in my django views.py I wrote and api call for posting a message

views.py:

def index2(request):
    url = 'https://slack.com/api/users.list'
    headers = {'Authorization' : 'Bearer {}'.format(SECRET)}
    r = requests.get(url, headers=headers)
    response = json.loads(r.text)
    print(response)
    all_ids = [member['id'] for member in response['members'] if not member['deleted']]
    all_names = [member['name'] for member in response['members'] if not member['deleted']]
    my_list=zip(all_ids,all_names)
    value1 = request.GET.get('id')
    value2 = request.GET.get('msg')
    print(value1,value2)
    client = WebClient(token=SECRET)
    try:
        response = client.chat_postMessage(
            channel=value1,text=value2)
        assert response["message"]["text"] == value2
    except SlackApiError as e:
        assert e.response["ok"] is False
        assert e.response["error"]  # str like 'invalid_auth', 'channel_not_found'
        print(f"Got an error: {e.response['error']}")

Here the problem the user who logged in with signinslack is not able to message to his desired slack users every time whoever logs in it is posting messages to desired ones as me who is the creator of the slack app in api.slack.com Can anyone help me I am totally confused what is happening Thanks in advance!

for example if you signinwithslack in my project and if you message to anyone in the list of users the message is being received in the receiver side as the message is being posted by me...

1 answer

  • answered 2020-05-31 16:36 Erik Kalkoken

    Sign-in-with-Slack is only meant to authenticate Slack users to external website. Is not meant to provide a token to send messages to a Slack workspace or have any other interaction with the Slack workspace.

    If you want to send a message to a Slack workspace you need to have your Slack app installed to that workspace. That will yield you a bot token or user token, which you can use to send messages.

    See also this question: add to slack vs sign in with slack