-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from jaARke/main
Reduced cold start latency
- Loading branch information
Showing
11 changed files
with
96 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
# The following are lambda environment variables: | ||
# The following variables should be added as GitHub secrets | ||
APP_ID=DISCORD_APP_ID | ||
PUBLIC_KEY=DISCORD_PUBLIC_KEY | ||
BOT_TOKEN=DISCORD_BOT_TOKEN | ||
NEWS_API_KEY=NEWS_API_KEY | ||
FINNHUBB_API_KEY=FINNHUBB_API_KEY | ||
|
||
# The following are GitHub secrets: | ||
AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID # S3 bucket access key | ||
AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY # S3 bucket secret key | ||
AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY # S3 bucket secret key | ||
NEWS_API_KEY=NEWS_API_KEY | ||
FINNHUB_API_KEY=FINNHUB_API_KEY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
import pickle | ||
from .CommandRegistry import CommandRegistry | ||
|
||
registry = CommandRegistry(command_dir="commands", app_id=os.environ.get('APP_ID'), bot_token=os.environ.get('BOT_TOKEN')) | ||
|
||
with open("CommandRegistry.pickle", "wb") as f: | ||
pickle.dump(registry, f, protocol=pickle.HIGHEST_PROTOCOL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from .CommandArg import CommandArg | ||
from .InteractionHandler import InteractionHandler | ||
from .Interaction import Interaction, Embedding | ||
from .CommandRegistry import CommandRegistry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
import os | ||
from discord_lambda import InteractionHandler | ||
from nacl.signing import VerifyKey | ||
from discord_lambda import Interaction, Embedding | ||
import pickle | ||
|
||
|
||
def verify_signature(event: dict) -> None: | ||
raw_body = event.get("rawBody") | ||
auth_sig = event['params']['header'].get('x-signature-ed25519') | ||
auth_ts = event['params']['header'].get('x-signature-timestamp') | ||
|
||
message = auth_ts.encode() + raw_body.encode() | ||
verify_key = VerifyKey(bytes.fromhex(os.environ.get('PUBLIC_KEY'))) | ||
verify_key.verify(message, bytes.fromhex(auth_sig)) | ||
|
||
handler = InteractionHandler(command_dir="commands", app_id=os.environ.get("APP_ID"), public_key=os.environ.get("PUBLIC_KEY"), bot_token=os.environ.get("BOT_TOKEN")) | ||
|
||
def lambda_handler(event, context): | ||
handler.handle(event) | ||
try: | ||
verify_signature(event) | ||
except Exception as e: | ||
# Return a 401 Unauthorized response | ||
raise Exception(f"[UNAUTHORIZED] Invalid request signature: {e}") | ||
|
||
interaction = Interaction(event.get("body-json"), os.environ.get('APP_ID')) | ||
|
||
if interaction.type == 1: | ||
interaction.ping_response() | ||
|
||
elif interaction.type == 2: | ||
interaction.defer(ephemeral=True) | ||
|
||
registry = pickle.load(open("/opt/CommandRegistry.pickle", "rb")) | ||
try: | ||
func, args = registry.find_func(interaction.data) | ||
func(interaction, **args) | ||
except Exception as e: | ||
interaction.send_response(embeds=[Embedding(":x: Error", f"The request could not be completed:\n`{e}`", color=0xFF0000)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters