Skip to content

Commit

Permalink
Add history feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Sardor Muminov committed Jul 26, 2017
1 parent b21d1c8 commit 41c75de
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 4 additions & 8 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Queries
from database.queries import user_exists, insert_user, deactivate_user
from database.queries import insert_text

# Variables
api_token = os.environ.get('API_TOKEN')
Expand Down Expand Up @@ -148,12 +149,7 @@ async def left_chat_member_event(chat, member):
@bot.group_message
async def group_message_event(chat, message):
sender = message.get('from')
sender_id = sender.get('id') # noqa
sender_name = sender.get('first_name')
chat = message.get('chat')
group_id = chat.get('id')
message_date = message.get('date')
message_text = message.get('text')
logger.info('Got message from group %s at %s', group_id, message_date)
logger.info('Sender %s', sender_name)
logger.info('Text -> %s', message_text)
text = message.get('text')
logger.info('Got message from group at %s', message_date)
await insert_text(chat.bot.pg_pool, sender, text)
15 changes: 15 additions & 0 deletions database/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ async def deactivate_user(pool, user):

finally:
await pool.release(conn)


async def insert_text(pool, sender, text):
query = '''
insert into history(sender, text)
values ($1, $2)
'''

conn = await pool.acquire()

try:
await conn.execute(query, sender, text)

finally:
await pool.release(conn)
2 changes: 1 addition & 1 deletion database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ create table if not exists users (


create table if not exists history (
id bigint primary key,
id bigserial primary key,
sender jsonb,
timestamp timestamptz default timezone('Asia/Tashkent'::text, now()),
text citext
Expand Down

0 comments on commit 41c75de

Please sign in to comment.