discord / discord-interactions-python Goto Github PK
View Code? Open in Web Editor NEWUseful tools for building interactions in Python
License: MIT License
Useful tools for building interactions in Python
License: MIT License
Currently, if the method wrapped by the verify_key_decorator
decorator receives a request that doesn't contain either of the X-Signature-*
headers, or a body without the type
key, it will result in an exception that will trigger a 500 error. This isn't particularly problematic given that requests from any source other than Discord should be rejected anyway, but it would be preferable to respond with a 403 error instead so that these errors are not included in the logs of the application.
@verify_key_decorator(CLIENT_PUBLIC_KEY)
def interactions():
if request.json['type'] == InteractionType.APPLICATION_COMMAND:
return jsonify({
'type': InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
'data': {
'content': 'Hello world'
}
})
In the case of the missing headers, the exception may look something like this (Though the traceback probably won't include the google cloud stuff):
Traceback (most recent call last):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 66, in view_func
return function(request._get_current_object())
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/discord_interactions/__init__.py", line 44, in __decorator
if not verify_key(request.data, signature, timestamp, client_public_key):
File "/layers/google.python.pip/pip/lib/python3.8/site-packages/discord_interactions/__init__.py", line 24, in verify_key
message = timestamp.encode() + raw_body
AttributeError: 'NoneType' object has no attribute 'encode'
The NoneType
in this case is the None
returned by request.headers.get('X-Signature-Timestamp')
since that header is not present in the request.
Would it be in scope for this project to add a decorator to update Discord with changes to slash commands when the flask service is started/reloaded in debug mode for faster development? There would likely need to be some way to store bot token as authentication in the flask app, maybe a wrapper of some kind. as an example:
from discord_interactions import verify_key_decorator, SlashCommand
app = Flask(__name__)
sc = SlashCommand(app)
sc.token = environ["BOT_TOKEN"] # also retrieves application client ID
sc.guild_id = guild_id # optional, limits all commands on this service to a guild, would also be configurable from each decorator
def get_opts(name):
with open("options.json") as f
return json.load(f)[name]
@app.route("/", methods=["POST"])
@verify_key_decorator(PUBKEY) # could be implicit with inclusion of SlashCommand.command
@sc.command(name="watch", description=long_desc, options=get_opts("watch")) # , guild_id=guild_id)
def watch():
pass
If this type of behaviour is not in scope I'll start up a separate project to provide this functionality.
The raw_body parameter type should be bytes and not str.
I'm trying to add my interactions endpoint url to my app in discord, using a python flask app.
I'm running the example in the README.md and there is a failure when verifying the content of the type :
x.x.x.x - - [26/Aug/2021 16:26:43] "POST /interactions HTTP/1.1" 401 -
[2021-08-26 16:26:43,604] ERROR in app: Exception on /interactions [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/discord_interactions/__init__.py", line 48, in __decorator
if request.json and request.json.get('type') == InteractionType.PING:
AttributeError: 'tuple' object has no attribute 'get'
x.x.x.x - - [26/Aug/2021 16:26:43] "POST /interactions HTTP/1.1" 500 -
How can I add my interactions endpoint url if discord can't verify it ?
Tested with Python 3.7 and 3.8.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.