Advertisement
Guest User

Untitled

a guest
Mar 4th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. from slackbot.bot import Bot
  2. from slackbot.bot import respond_to
  3. from slackbot.bot import listen_to
  4. import re
  5. import psycopg2
  6.  
  7. try:
  8. conn = psycopg2.connect("dbname='postgres' user='postgres' host='localhost' password='postgres'")
  9. cur = conn.cursor()
  10.  
  11. except:
  12. print ('Couldn\'t connect to the database')
  13.  
  14. @respond_to('help', re.IGNORECASE)
  15. def help(message):
  16. message.reply('I am Gunnery Sergeant Hartman, your senior drill instructor. I will drill you to satisfy your habits!')
  17. message.reply('Type `start habit` to start tracking a new habit.')
  18. message.reply('Type `all habits` to list all habits you\'re tracking.')
  19.  
  20. @respond_to('start habit (.*)', re.IGNORECASE)
  21. def start(message, habit):
  22. # Save username to users table
  23. current_user = str(message.channel._client.users[message.body['user']][u'name'])
  24. # Only add user to table if he doesn't exist yet
  25. cur.execute("INSERT INTO users (Name) SELECT '{0}' WHERE NOT EXISTS (SELECT Name FROM users WHERE Name = '{0}');".format(current_user))
  26. conn.commit()
  27. message.reply('I love you too private Snowball!')
  28.  
  29. # List all users in the database
  30. @respond_to('list users', re.IGNORECASE)
  31. def start(message):
  32. cur.execute("SELECT * FROM users")
  33. users = cur.fetchall()
  34. message.reply('I found these losers in my camp:')
  35. for user in users:
  36. message.reply(user[1])
  37.  
  38. def main():
  39. bot = Bot()
  40. bot.run()
  41.  
  42. if __name__ == "__main__":
  43. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement