Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def userExists(user):
- statement = f"SELECT EXISTS(SELECT 1 FROM slackDB.Assets WHERE userID LIKE '%{user}%')Assets"
- tempBool = cursor.execute(statement, args=None)
- conn.commit()
- return tempBool
- ################################
- # Slack Lambda handler.
- ################################
- import sys
- import logging
- import os
- import pymysql
- import urllib
- # Grab data from the environment.
- BOT_TOKEN = os.environ["BOT_TOKEN"]
- ASSET_TABLE = os.environ["ASSET_TABLE"]
- REGION_NAME = os.getenv('REGION_NAME', 'us-east-2')
- DB_NAME = "admin"
- DB_PASSWORD = "useradmin101"
- DB_DATABASE = "slackDB"
- RDS_HOST = "myslackdb.cb6yydo2b9mv.us-east-2.rds.amazonaws.com"
- port = 3306
- logger = logging.getLogger()
- logger.setLevel(logging.INFO)
- try:
- conn = pymysql.connect(RDS_HOST, user=DB_NAME, passwd=DB_PASSWORD, db=DB_DATABASE, connect_timeout=5)
- cursor = conn.cursor()
- except:
- logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
- sys.exit()
- # Define the URL of the targeted Slack API resource.
- SLACK_URL = "https://slack.com/api/chat.postMessage"
- def userExists(user):
- statement = f"SELECT EXISTS(SELECT 1 FROM slackDB.Assets WHERE userID LIKE '%{user}%')Assets"
- tempBool = cursor.execute(statement, args=None)
- conn.commit()
- return tempBool
- def addUser(user):
- statement = f"INSERT INTO `slackDB`.`Assets` (`userID`, `money`) VALUES ('{user}', '1000')"
- tempBool = cursor.execute(statement, args=None)
- conn.commit()
- return tempBool
- def lambda_handler(data, context):
- # Slack challenge answer.
- if "challenge" in data:
- return data["challenge"]
- # Grab the Slack channel data.
- slack_event = data['event']
- slack_userID = slack_event["user"]
- slack_text = slack_event["text"]
- channel_id = slack_event["channel"]
- slack_reply = ""
- # Ignore bot messages.
- if "bot_id" in slack_event:
- slack_reply = ""
- else:
- # Start data sift.
- if slack_text.startswith("!networth"):
- slack_reply = "Your networth is: "
- elif slack_text.startswith("!price"):
- command,asset = text.split()
- slack_reply = f"The price of a(n) {asset} is: "
- elif slack_text.startswith("!addme"):
- if userExists(slack_userID):
- slack_reply = f"User {slack_userID} already exists"
- else:
- slack_reply = f"Adding user {slack_userID}"
- addUser(slack_userID)
- # We need to send back three pieces of information:
- data = urllib.parse.urlencode(
- (
- ("token", BOT_TOKEN),
- ("channel", channel_id),
- ("text", slack_reply)
- )
- )
- data = data.encode("ascii")
- # Construct the HTTP request that will be sent to the Slack API.
- request = urllib.request.Request(
- SLACK_URL,
- data=data,
- method="POST"
- )
- # Add a header mentioning that the text is URL-encoded.
- request.add_header(
- "Content-Type",
- "application/x-www-form-urlencoded"
- )
- # Fire off the request!
- urllib.request.urlopen(request).read()
- # Everything went fine.
- return "200 OK"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement