This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_service.py
95 lines (68 loc) · 2.46 KB
/
api_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import logging.handlers
from datetime import datetime
import os
import boto3
import json
from flask import request
logger = logging.getLogger(__name__)
level = logging.getLevelName('INFO')
logger.setLevel(level)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
sout_handler = logging.StreamHandler()
sout_handler.setLevel(level)
sout_handler.setFormatter(formatter)
logger.addHandler(sout_handler)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1', aws_access_key_id=os.environ.get('aws_access_key'),
aws_secret_access_key=os.environ.get('aws_secret_access_key'))
def generate_epoch():
return int(datetime.now().replace(microsecond=0, second=0).strftime('%s'))
def put_conversation_item(message, chat_id):
try:
table = dynamodb.Table('Conversations')
logger.info(f'Adding detail for chatID, {chat_id}: {message}')
message_item = {
'conversation_id': chat_id,
'message_sent_on': generate_epoch(),
'message': message
}
response = table.put_item(Item=message_item)
status_code = response['ResponseMetadata'].get('HTTPStatusCode')
logger.info(f'Dynamo Response Code: {status_code}')
if status_code != 200:
raise IOError('Unable to put item into DynamoDB')
return message_item
except Exception as e:
logger.exception(e)
raise e
def get_conversation_item(chat_id):
try:
table = dynamodb.Table('Conversations')
chat = {
'conversation_id': chat_id
}
h = table.get_item(Key=chat)
print(h)
except Exception as e:
logger.exception(e)
raise e
def provision_chat(company_id, user):
try:
table = dynamodb.Table('Conversations')
logger.info(f'Adding detail for chatID, {chat_id}: {message}')
message_item = {
'conversation_id': chat_id,
'message_sent_on': generate_epoch(),
'message': message
}
response = table.put_item(Item=message_item)
status_code = response['ResponseMetadata'].get('HTTPStatusCode')
logger.info(f'Dynamo Response Code: {status_code}')
if status_code != 200:
raise IOError('Unable to put item into DynamoDB')
return message_item
except Exception as e:
logger.exception(e)
raise e
def close_chat():
msg = 'closed chat'
return msg