Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. def userExists(user):
  2. statement = f"SELECT EXISTS(SELECT 1 FROM slackDB.Assets WHERE userID LIKE '%{user}%')Assets"
  3. tempBool = cursor.execute(statement, args=None)
  4. conn.commit()
  5. return tempBool
  6.  
  7. ################################
  8. # Slack Lambda handler.
  9. ################################
  10.  
  11. import sys
  12. import logging
  13. import os
  14. import pymysql
  15. import urllib
  16.  
  17. # Grab data from the environment.
  18. BOT_TOKEN = os.environ["BOT_TOKEN"]
  19. ASSET_TABLE = os.environ["ASSET_TABLE"]
  20. REGION_NAME = os.getenv('REGION_NAME', 'us-east-2')
  21.  
  22. DB_NAME = "admin"
  23. DB_PASSWORD = "useradmin101"
  24. DB_DATABASE = "slackDB"
  25. RDS_HOST = "myslackdb.cb6yydo2b9mv.us-east-2.rds.amazonaws.com"
  26. port = 3306
  27.  
  28. logger = logging.getLogger()
  29. logger.setLevel(logging.INFO)
  30.  
  31. try:
  32. conn = pymysql.connect(RDS_HOST, user=DB_NAME, passwd=DB_PASSWORD, db=DB_DATABASE, connect_timeout=5)
  33. cursor = conn.cursor()
  34. except:
  35. logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
  36. sys.exit()
  37.  
  38. # Define the URL of the targeted Slack API resource.
  39. SLACK_URL = "https://slack.com/api/chat.postMessage"
  40.  
  41. def userExists(user):
  42. statement = f"SELECT EXISTS(SELECT 1 FROM slackDB.Assets WHERE userID LIKE '%{user}%')Assets"
  43. tempBool = cursor.execute(statement, args=None)
  44. conn.commit()
  45. return tempBool
  46.  
  47. def addUser(user):
  48. statement = f"INSERT INTO `slackDB`.`Assets` (`userID`, `money`) VALUES ('{user}', '1000')"
  49. tempBool = cursor.execute(statement, args=None)
  50. conn.commit()
  51. return tempBool
  52.  
  53. def lambda_handler(data, context):
  54. # Slack challenge answer.
  55. if "challenge" in data:
  56. return data["challenge"]
  57.  
  58. # Grab the Slack channel data.
  59. slack_event = data['event']
  60. slack_userID = slack_event["user"]
  61. slack_text = slack_event["text"]
  62. channel_id = slack_event["channel"]
  63. slack_reply = ""
  64.  
  65. # Ignore bot messages.
  66. if "bot_id" in slack_event:
  67. slack_reply = ""
  68. else:
  69. # Start data sift.
  70. if slack_text.startswith("!networth"):
  71. slack_reply = "Your networth is: "
  72. elif slack_text.startswith("!price"):
  73. command,asset = text.split()
  74. slack_reply = f"The price of a(n) {asset} is: "
  75. elif slack_text.startswith("!addme"):
  76. if userExists(slack_userID):
  77. slack_reply = f"User {slack_userID} already exists"
  78. else:
  79. slack_reply = f"Adding user {slack_userID}"
  80. addUser(slack_userID)
  81.  
  82. # We need to send back three pieces of information:
  83. data = urllib.parse.urlencode(
  84. (
  85. ("token", BOT_TOKEN),
  86. ("channel", channel_id),
  87. ("text", slack_reply)
  88. )
  89. )
  90. data = data.encode("ascii")
  91.  
  92. # Construct the HTTP request that will be sent to the Slack API.
  93. request = urllib.request.Request(
  94. SLACK_URL,
  95. data=data,
  96. method="POST"
  97. )
  98. # Add a header mentioning that the text is URL-encoded.
  99. request.add_header(
  100. "Content-Type",
  101. "application/x-www-form-urlencoded"
  102. )
  103.  
  104. # Fire off the request!
  105. urllib.request.urlopen(request).read()
  106.  
  107. # Everything went fine.
  108. return "200 OK"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement