Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from slackbot.bot import Bot
- from slackbot.bot import respond_to
- from slackbot.bot import listen_to
- import re
- import psycopg2
- try:
- conn = psycopg2.connect("dbname='postgres' user='postgres' host='localhost' password='postgres'")
- cur = conn.cursor()
- except:
- print ('Couldn\'t connect to the database')
- @respond_to('help', re.IGNORECASE)
- def help(message):
- message.reply('I am Gunnery Sergeant Hartman, your senior drill instructor. I will drill you to satisfy your habits!')
- message.reply('Type `start habit` to start tracking a new habit.')
- message.reply('Type `all habits` to list all habits you\'re tracking.')
- @respond_to('start habit (.*)', re.IGNORECASE)
- def start(message, habit):
- # Save username to users table
- current_user = str(message.channel._client.users[message.body['user']][u'name'])
- # Only add user to table if he doesn't exist yet
- cur.execute("INSERT INTO users (Name) SELECT '{0}' WHERE NOT EXISTS (SELECT Name FROM users WHERE Name = '{0}');".format(current_user))
- conn.commit()
- message.reply('I love you too private Snowball!')
- # List all users in the database
- @respond_to('list users', re.IGNORECASE)
- def start(message):
- cur.execute("SELECT * FROM users")
- users = cur.fetchall()
- message.reply('I found these losers in my camp:')
- for user in users:
- message.reply(user[1])
- def main():
- bot = Bot()
- bot.run()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement