Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #9 from ttt246/feature/twilio_7
Browse files Browse the repository at this point in the history
Feature/twilio 7
  • Loading branch information
Thomas Richardson committed May 30, 2023
2 parents 47a23ef + 4dace36 commit 98537f1
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .sauce/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ suites:
- 02 Upload Image
- 03 Image Relatedness
- 04 Add Feedback
- 05 Get Feedback
- 05 Get Feedback
- 06 Get Commands
- 07 Rising Chat
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ All credentials including openai, replicate and pinecone are shared with Github
- Integrated all Unit Tests
- Integrated Deployment Workflow

## Contributing
Please refer to each project's style and contribution guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow.

1. **Fork** the repo on GitHub
2. **Clone** the project to your own machine
3. **Commit** changes to your own branch
4. **Push** your work back up to your fork
5. Submit a **Pull request** so that we can review your changes

NOTE: Be sure to merge the latest from "upstream" before making a pull request!
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ Werkzeug==2.2.3
win32-setctime==1.1.0
wrapt==1.15.0
yarl==1.8.2
twilio==8.2.1
rising_plugin==0.1.0
16 changes: 16 additions & 0 deletions sauce_tests/07 Rising Chat/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- id: global
children:
- id: variable
name: protocol
value: https://
- id: variable
name: domain
value: smartphone.herokuapp.com
- id: variable
name: endpoint
value: /chat_rising
- id: sets
children:
- id: set
children: []
name: default
38 changes: 38 additions & 0 deletions sauce_tests/07 Rising Chat/unit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
assertions:
- id: post
children:
- id: body
contentType: application/json
content: >-
{
"token": "test_token",
"uuid": "test_uuid",
"history": [{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Who won the world series in 2020?"
},
{
"role": "assistant",
"content": "The Los Angeles Dodgers won the World Series in 2020."
}
],
"user_input": "Where was it played?",
"model": "gpt-3.5-turbo"
}
url: ${protocol}${domain}${endpoint}
var: payload
mode: json
- id: assert-equals
expression: payload_response.headers['Content-Type']
value: application/json
- id: assert-exists
expression: payload
- id: assert-exists
expression: payload.message
- id: assert-exists
expression: payload.result
configs: []
8 changes: 8 additions & 0 deletions src/common/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from src.model.basic_model import BasicModel
from src.model.message_model import MessageModel
from src.model.sms_model import SMSModel


class Assembler:
Expand Down Expand Up @@ -33,3 +34,10 @@ def to_array_message_model(self, data: Any) -> List[MessageModel]:

def to_message_model(self, data: Any) -> MessageModel:
return MessageModel(data["role"], data["content"])

"""mapping data to a SMSModel"""

def to_sms_model(self, data: Any) -> SMSModel:
sms_model = SMSModel()
sms_model.get_sms_model(data)
return sms_model
5 changes: 5 additions & 0 deletions src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
COMMAND_SMS_INDEXS = [4, 5]


# Twilio
ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")


def get_firebase_cred():
if os.path.exists("firebase_cred.json"):
file = open("firebase_cred.json")
Expand Down
14 changes: 14 additions & 0 deletions src/model/sms_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""sms message model includes from, to and body"""
from typing import Any


class SMSModel:
def __init__(self, _from="", _to="", body=""):
self._from = _from
self._to = _to
self.body = body

def get_sms_model(self, data: Any) -> None:
self._from = data["from"]
self._to = data["to"]
self.body = data["body"]
31 changes: 31 additions & 0 deletions src/router/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from src.service.command_service import CommandService
from src.service.feedback_service import FeedbackService
from src.service.llm.chat_service import ChatService
from src.service.twilio_service import TwilioService


def construct_blueprint_api(generator):
Expand Down Expand Up @@ -250,4 +251,34 @@ def message_agent():
),
)

@generator.request_body(
{
"token": "test_token",
"uuid": "test_uuid",
"data": {
"from": "+15005550006",
"to": "+12173748105",
"body": "All in the game, yo",
},
}
)
@generator.response(
status_code=200, schema={"message": "message", "result": "test_result"}
)
@api.route("/send_sms", methods=["POST"])
def send_sms():
try:
data = json.loads(request.get_data())
token = data["token"]
uuid = data["uuid"]

# parsing feedback payload
sms_model = assembler.to_sms_model(data["data"])
# send sms via twilio
twilio_service = TwilioService()
twilio_resp = twilio_service.send_sms(sms_model)
except Exception as e:
return assembler.to_response(400, "Failed to send sms", "")
return assembler.to_response(200, "Sent a sms successfully", twilio_resp.sid)

return api
24 changes: 24 additions & 0 deletions src/service/twilio_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""service for sms using twilio apis"""
import json
from typing import Any

from twilio.rest import Client

from src.common.utils import ACCOUNT_SID, AUTH_TOKEN
from src.logs import logger
from src.model.sms_model import SMSModel


class TwilioService:
def __init__(self):
self.client = Client(ACCOUNT_SID, AUTH_TOKEN)

def send_sms(self, data: SMSModel) -> Any:
message = self.client.messages.create(
body=data.body, from_=data._from, to=data._to
)
logger.info(
message=message.sid,
title="sent twilio sms",
)
return message
57 changes: 56 additions & 1 deletion src/static/swagger.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.1
info:
title: Application
description: Generated at 28/05/2023 17:05:42. This is the swagger
description: Generated at 30/05/2023 05:40:23. This is the swagger
ui based on the open api 3.0 specification of the Application
version: 1.0.0 created by the flask swagger generator.
externalDocs:
Expand Down Expand Up @@ -109,6 +109,16 @@ paths:
responses:
'200':
$ref: '#/components/responses/message_agent_response'
'/send_sms':
post:
tags:
- send_notification
operationId: 'send_sms'
requestBody:
$ref: '#/components/requestBodies/send_sms_request_body'
responses:
'200':
$ref: '#/components/responses/send_sms_response'
components:
requestBodies:
send_notification_request_body:
Expand Down Expand Up @@ -146,6 +156,13 @@ components:
application/json:
schema:
$ref: '#/components/schemas/message_agent'
send_sms_request_body:
description: None
required: True
content:
application/json:
schema:
$ref: '#/components/schemas/send_sms'
responses:
send_notification_response:
description: send_notification response
Expand Down Expand Up @@ -195,6 +212,12 @@ components:
application/json:
schema:
$ref: '#/components/schemas/message_agent_response_schema'
send_sms_response:
description: send_sms response
content:
application/json:
schema:
$ref: '#/components/schemas/send_sms_response_schema'
schemas:
send_notification:
type: object
Expand Down Expand Up @@ -376,3 +399,35 @@ components:
model:
type: string
example: gpt-3.5-turbo
send_sms_response_schema:
type: object
properties:
message:
type: string
example: message
result:
type: string
example: test_result
send_sms:
type: object
properties:
token:
type: string
example: test_token
uuid:
type: string
example: test_uuid
data:
$ref: "#/components/schemas/data_send_sms"
data_send_sms:
type: object
properties:
from:
type: string
example: +15005550006
to:
type: string
example: +12173748105
body:
type: string
example: All in the game, yo

0 comments on commit 98537f1

Please sign in to comment.