slack API - reaction_added - python

I'm using following slack events python adapter:
https://github.com/slackapi/python-slack-events-api
And the code from the tutorial doesn't work:
from flask import Flask
from slackeventsapi import SlackEventAdapter
# This `app` represents your existing Flask app
app = Flask(__name__)
# An example of one of your Flask app's routes
#app.route("/")
def hello():
return "Hello there!"
# Bind the Events API route to your existing Flask app by passing the server
# instance as the last param, or with `server=app`.
slack_events_adapter = SlackEventAdapter(SLACK_SIGNING_SECRET, "/slack/events", app)
# Create an event listener for "reaction_added" events and print the emoji name
#slack_events_adapter.on("reaction_added")
def reaction_added(event_data):
emoji = event_data["event"]["reaction"]
print(emoji)
# Start the server on port 3000
if __name__ == "__main__":
app.run(port=3000)
It doesn't react on any emojies. It looks like putting emoji is not an event...

Resolved.
From api.slack.com -> bot page -> Event Subscriptions -> Subscrive to Bot Events -> add "reaction_add"

Related

Multiple suscribers to SSE being blocked via pubsub with Flask

I'm creating a live data table page using Flask. The data shown is coming from a PostgreSQL database.
I am passing the new data being inserted into the database using the pubsub mechanism, and the operation is then transmitted using SSE to the front end Flask application.
Everything is working fine when I am opening one window but when I'm opening another window (to simulate multiple users), only the most recently loaded window receives the SSE. What am I doing wrong?
I tried multiple solutions, from using Flask in threaded mode to using gevent or gunicorn
Here is my code:
main.py
from gevent import monkey
monkey.patch_all()
import psycopg2, pgpubsub
from flask import Flask, render_template, Response
from gevent.pywsgi import WSGIServer
import time
app = Flask(__name__)
pubsub = pgpubsub.connect(database="postgres"....)
def get_connection():
try:
return psycopg2.connect(database="postgres.....")
except Exception as e:
return f"Error connecting to DB: {e}"
#app.route('/')
def home():
return render_template('index.html')
#app.route('/events')
def events():
def update_pusher():
print('Started listening')
pubsub.listen('data_changed')
while True:
for event in pubsub.events(yield_timeouts=True):
if event is None:
pass
else:
yield f"data: {event.payload}\nevent: online\n\n"
time.sleep(0.01)
return Response(
response=update_pusher(),
mimetype='text/event-stream'
)
if __name__ == "__main__":
http_server = WSGIServer(("localhost", 5003), app)
http_server.serve_forever()
index.html
<p>This list is populated by server side events.</p>
<ul id="list"></ul>
<script>
var eventSource = new EventSource("/events")
eventSource.addEventListener("online", function(e) {
// console.log(e.data.color)
data = JSON.parse(e.data)
const li = document.createElement('li')
li.innerText = data
list.appendChild(li)
}, false)

Slack events adaptor not giving a proper response

I was trying to make a slack bot using slackeventsapi running on ngrok for now.
It can send messages properly but slack events adaptor doesn't seem to be working properly. It gives the code 200 every time a message is sent but the payload doesn't come. I tried printing it but the printing it shows nothing.
There was another post asking a similar question whose solution in the end was to make a new app on slack API but it doesn't seem to fix my issue. I have made another app but the issue persists.
I was following a tutorial so I have tried to match his code exactly but it doesn't seem to work even then. In case it will be helpful - https://www.youtube.com/watch?v=6gHvqXrfjuo&list=PLzMcBGfZo4-kqyzTzJWCV6lyK-ZMYECDc&index=2.
The slack API scopes
Slack API Subscriptions
import slack
import os
from pathlib import Path
from dotenv import load_dotenv
from flask import Flask
from slackeventsapi import SlackEventAdapter
env_path = Path('.')/'.env'
load_dotenv(dotenv_path=env_path)
client = slack.WebClient(token=os.environ['TEST2_SLACK_TOKEN'])
BOT_ID = client.api_call("auth.test")['user_id']
app = Flask(__name__)
slack_event_adaptor = SlackEventAdapter(os.environ['SIGNING_SECRET2'], '/slack/events', app)
client.chat_postMessage(channel=f'#new', text="Hello")
if __name__ == "__main__":
app.run(debug=True)
#slack_event_adaptor.on('message')
def message(payload):
print(payload)
event = payload.get('event',{})
channel_id = event.get('channel')
user_id = event.get('user')
text = event.get('text')
if BOT_ID != user_id:
client.chat_postMessage(channel= channel_id, text = text)
I had similar problem when I used slack_event_adaptor and then I tried slack_bolt and everything works well. Let me share example you may try if you want:
import re
from config import config
from flask import Flask, request
from slack_sdk import WebClient
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
app = Flask(__name__)
slack_token = config.slack_token
client = WebClient(slack_token)
bolt_app = App(token=slack_token, signing_secret=config.signing_secret)
handler = SlackRequestHandler(bolt_app)
#bolt_app.message(re.compile("Hello bot",re.IGNORECASE))
def reply_in_thread(payload: dict):
""" This will reply in thread instead of creating a new thread """
response = client.chat_postMessage(channel=payload.get('channel'),
thread_ts=payload.get('ts'),
text=f"Hello<#{payload['user']}>")
#app.route("/datalake/events", methods=["POST"])
def slack_events():
""" Declaring the route where slack will post a request """
return handler.handle(request)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
When you write "Hello bot" bot will respond you accordingly.

How to create an API server with endpoints in Python?

Using Python, I would like to create an API server with the following endpoints:
/metric - needs to return the number of times the API server has been called
/health - needs to return ok
Since you don't specify any framework ou give any initial code, here's a simple example using Flask on how it would be:
from flask import Flask
app = Flask(__name__)
count = 0
#app.route('/metric')
def metric():
global count
count += 1
return str(count)
#app.route('/healthz')
def health():
return "ok"
app.run()
To install Flask, run:
pip3 install flask
Run the python code and access on your browser http://127.0.0.1:5000/metric and http://127.0.0.1:5000/healthz
FastAPI is a great option. FastAPI is a "fast (high-performance), web framework for building APIs". It provides interactive API documentation (provided by Swagger UI) to visualize and interact with the API’s resources.
Working Example
You can access the interactive API autodocs at http://127.0.0.1:8000/docs. You can also access the API endpoints directly from the browser at, for instance, http://127.0.0.1:8000/metric.
import uvicorn
from fastapi import FastAPI
app = FastAPI()
hits = 0
#app.get("/metric")
async def metric():
global hits
hits+=1
return {"hits": hits}
#app.get("/health")
async def health():
return "ok"
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)

Slack Python Listening Events

I'm trying to learn how to crate Slack Apps using the Slack Bolt Framewok, so I'm studying this tutorial (https://api.slack.com/start/building/bolt-python). I followed all the steps but my bot is not listening to the events.
The python class:
import os
from slack_bolt import App
app = App(
token = 'xoxb-xxxxxxxx-2463004875172-0pnMlWzr30pFg2vFGtt52wzx',
signing_secret = 'xxxxxxx3eba2b1fc11bf6076'
)
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
#app.event("message")
def handle_message_events(body, logger):
logger.info(body)
The response I get when I write a message to the bot:
response
The response in the ngrok:
ngrok response
The bot events subscriptions
subscriptions
Does anyone know what I am forgetting to do?
As per the official documentation, functionality goes before you start the app.
https://github.com/slackapi/bolt-python#creating-an-app
app = App()
# Add functionality here
if __name__ == "__main__":
app.start(3000) # POST http://localhost:3000/slack/events
Can you move following code accordingly, and check?
#app.event("message")
def handle_message_events(body, logger):
logger.info(body)

How would I get the output of a function from a different running python script?

I currently am running two separate python scripts, one for a Flask webapp and the other for a discord.py bot. Upon receiving an incoming request on the flask webapp, I would like to call a function in the discord.py script and return the its output to the view.
Currently both scripts are blocking so I am interested in using a redis messaging queue to pass requests between each other (something like the kombu package).
webapp.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
user = get_user(123456789) # get_user function from bot.py
return "Hello, {0}".format(user.name)
if __name__ == "__main__":
app.run()
bot.py:
import discord
client = discord.Client()
async def get_user(user_id):
user = await client.get_user_info(user_id)
return user
if __name__ == "__main__":
client.run("bot token")
I am not sure how to accomplish this task or if Kombu is really the way to go. How would I implement to my flask webapp so that I can call functions within my discord.py bot?
Use import function from other file
from bot import get_user
and make sure, you have both file in same directory.

Categories