Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build a Slack app using message event subscription #1157

Closed
utsang opened this issue Jan 3, 2022 · 5 comments
Closed

Build a Slack app using message event subscription #1157

utsang opened this issue Jan 3, 2022 · 5 comments
Labels
question M-T: User needs support to use the project

Comments

@utsang
Copy link

utsang commented Jan 3, 2022

I was basically following video tutorial: https://www.youtube.com/watch?v=6gHvqXrfjuo&t=1030s
everything was working but until I started getting this error.

Following is the code:

env_path = Path('.')/ '.env'
load_dotenv(dotenv_path=env_path)


app = Flask(__name__)
slack_event_adapter = SlackEventAdapter(os.environ['SIGNING_SECRET'],'/slack/events',app)


client = slack.WebClient(token=os.getenv("SLACK_TOKEN"))


@slack_event_adapter.on('message')
def message(payload):
    event = payload.get('event', {})
    channel_id = event.get('chanel')
    user_id = event.get('user')
    text = event.get('text')
    client.chat_postMessage(channel=channel_id, text = text)

if __name__ == "__main__":
    app.run(debug=True)
@seratch seratch added question M-T: User needs support to use the project and removed untriaged labels Jan 3, 2022
@seratch
Copy link
Member

seratch commented Jan 3, 2022

channel_id = event.get('chanel')

This part is not correct. The key should be “channel”, not “chanel”.

@utsang
Copy link
Author

utsang commented Jan 4, 2022

channel_id = event.get('chanel')

This part is not correct. The key should be “channel”, not “chanel”.

Oh thank you very much can't belive I missed that, I had another issue with slack api, this is more of a logic issue: I have the following code:

Hello Everyone,
I am currently making a slack bot that can take ticker name of a certain stock from slack and output its information from yfinance. I have the following code:

client = slack.WebClient(token=os.getenv("SLACK_TOKEN"))
BOT_ID = client.api_call("auth.test")['user_id']

@slack_event_adapter.on('message')
def message(payload):
    event = payload.get('event', {})
    channel_id = event.get('channel')
    user_id = event.get('user')
    text = event.get('text')
    today = date.today()
    d4 = today.strftime("%Y-%m-%d")
    print(d4)

    tickerWant = text
    data = yf.download(tickerWant, start="2021-09-5", end=d4)

    data.to_csv(rf'C:\work\demo\data\{tickerWant}.csv')
   
    # if BOT_ID != user_id:
    #     client.chat_postMessage(channel=channel_id, text = 'print succesful')

so at first the code after tickerWant was in a different file, my understanding was that I could do from chart import message(chart.py is the main python that I had defined message into) that didn't work as I didn't know what to put in the payload argument. I have this code now in the main py file. The issue I am having currently is that once I get the csv file printed to my local storage, I want my code to post successful print to the slack message channel. As of now, it will only do that once I kill the program and if I use client.chat_postMessage() without the if statement with the botID then I repeatedly get the issue of me getting print successful in succession to my slack channel, I dont want that I only want it once its printed. This is an logic error that I don't know how to solve any help would be appreciated.

@seratch seratch changed the title Error using slack events api: The request to the Slack API failed. The server responded with: {'ok': False, 'error': 'invalid_arguments', 'response_metadata': {'messages': ['[ERROR] must provide a string [json-pointer:/channel]']}} Build a Slack app using message event subscription Jan 4, 2022
@seratch
Copy link
Member

seratch commented Jan 4, 2022

@utsang

posting a message several times

Your app might not be responding to requests from Slack within 3 seconds. If so, that can be one of the reasons that your app prints several times. In this scenario, your app receives the same request three times at maximum. For this reason, python-slack-events-api is not recommended for complex apps. Refer to slackapi/python-slack-events-api#84 (comment) for more details. Instead, we recommend using bolt-python; with this library, running time-consuming code in your listener is much easier. You can find many examples here: https://github.com/slackapi/bolt-python/tree/main/examples

if BOT_ID != user_id:

If you go with bolt-python, this if statement is no longer necessary. bolt-python framework automatically ignores the app itself's events for you.

I think that I've answered all your questions here. Would you mind closing this issue now?

Also, if you continue your app development with bolt-python, please feel free to ask questions here: https://github.com/slackapi/bolt-python/issues/new/choose

@utsang
Copy link
Author

utsang commented Jan 4, 2022

thank you

@seratch seratch closed this as completed Jan 4, 2022
@humzasalim
Copy link

Hi, I have similar code but it is correct. I can't connect my ngrok to flask, but it connects to the url request in slack api website.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

3 participants