Skip to content

Commit

Permalink
Merge pull request #2 from rubenLeapFinancial/main
Browse files Browse the repository at this point in the history
add parameter ssl = false
  • Loading branch information
alejamp authored Nov 28, 2024
2 parents f0d7e76 + 1924cbd commit d8f6f57
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
18 changes: 10 additions & 8 deletions celai_chatwoot/connector/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ def __init__(self,
base_url: str,
account_id: str,
access_key: str,
headers: Optional[Dict[str, str]] = None):
headers: Optional[Dict[str, str]] = None,
ssl: bool = False):
self.base_url = base_url
self.account_id = account_id
self.access_key = access_key
self.headers = headers or {}
self.headers.update({
'api_access_token': access_key
})
self.ssl = ssl

async def list_agent_bots(self) -> Dict[str, Any]:
url = f"{self.base_url}/api/v1/accounts/{self.account_id}/agent_bots"
log.debug(f"Listing agent bots from Chatwoot url: {url}")

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.get(url, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand All @@ -44,7 +46,7 @@ async def create_agent_bot(self,

payload = {k: v for k, v in payload.items() if v is not None}

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.post(url, json=payload, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand All @@ -53,7 +55,7 @@ async def delete_agent_bot(self, agent_bot_id: str) -> Dict[str, Any]:
url = f"{self.base_url}/api/v1/accounts/{self.account_id}/agent_bots/{agent_bot_id}"
log.debug(f"Deleting agent bot from Chatwoot url: {url}")

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.delete(url, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand All @@ -62,7 +64,7 @@ async def get_agent_bot(self, agent_bot_id: str) -> Dict[str, Any]:
url = f"{self.base_url}/api/v1/accounts/{self.account_id}/agent_bots/{agent_bot_id}"
log.debug(f"Getting agent bot from Chatwoot url: {url}")

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.get(url, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand All @@ -84,8 +86,8 @@ async def update_agent_bot(self,
}

payload = {k: v for k, v in payload.items() if v is not None}

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
#async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
async with session.patch(url, json=payload, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand Down Expand Up @@ -123,7 +125,7 @@ async def assign_bot_to_inbox(self, inbox_id: str | int, agent_bot_id: str | int
'agent_bot': agent_bot_id
}

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.post(url, json=payload, headers=self.headers) as response:
response_data = await response.json()
return response_data
Expand Down
12 changes: 7 additions & 5 deletions celai_chatwoot/connector/msg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ def __init__(self,
base_url: str,
account_id: str,
access_key: str,
headers: Optional[Dict[str, str]] = None):
headers: Optional[Dict[str, str]] = None,
ssl: bool = False):
self.base_url = base_url
self.account_id = account_id
self.access_key = access_key
self.headers = headers or {}
self.headers.update({
'api_access_token': access_key
})
self.ssl = ssl


async def __build_content(self, attach: ChatwootAttachment):
Expand All @@ -57,7 +59,7 @@ async def __build_content_image(self, content: Any):
b64_img = content.split("base64,")[1]
if content.startswith("http"):
# download the image
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.get(content) as resp:
b64_img = base64.b64encode(await resp.read()).decode()
if len(content) > 100:
Expand All @@ -78,7 +80,7 @@ async def __build_content_audio(self, content: str):
b64_audio = content.split("base64,")[1]
if content.startswith("http"):
# download the audio
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.get(content) as resp:
b64_audio = base64.b64encode(await resp.read()).decode()

Expand Down Expand Up @@ -124,7 +126,7 @@ async def send_text_message(
# Remove keys with None values
payload = {k: v for k, v in payload.items() if v is not None}

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
async with session.post(url, json=payload, headers=headers) as response:
response_data = await response.json()
return response_data
Expand Down Expand Up @@ -161,7 +163,7 @@ async def send_attachment(self, conversation_id, attach: ChatwootAttachment=None


# Make the HTTP request
async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=self.ssl)) as session:
try:
async with session.post(url, data=form, headers=self.headers) as response:
res = await response.json()
Expand Down
16 changes: 11 additions & 5 deletions celai_chatwoot/connector/woo_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def __init__(self,
chatwoot_url: str,
inbox_id: str,
bot_description: str = "Celai Bot",
stream_mode: StreamMode = StreamMode.SENTENCE):
stream_mode: StreamMode = StreamMode.SENTENCE,
ssl: bool = False):
log.debug("Creating Chatwoot connector")

self.router = APIRouter(prefix="/chatwoot")
Expand All @@ -52,6 +53,7 @@ def __init__(self,
self.inbox_id = inbox_id
self.chatwoot_url = chatwoot_url
self.bot_description = bot_description or "Celai generated Bot"
self.ssl = ssl


def __create_routes(self, router: APIRouter):
Expand Down Expand Up @@ -169,7 +171,8 @@ async def send_text_message(self, lead: WootLead, text: str, metadata: dict = {}
log.debug(f"Sending message to Chatwoot acc: {lead.account_id}, inbox: {lead.inbox_id}, conv: {lead.conversation_id}, private:{is_private}, text: {text}")
client = ChatwootMessages(base_url=self.chatwoot_url,
account_id=lead.account_id,
access_key=self.access_key)
access_key=self.access_key,
ssl=self.ssl)

await client.send_text_message(conversation_id=lead.conversation_id,
content=text,
Expand All @@ -193,7 +196,8 @@ async def send_image_message(self,

client = ChatwootMessages(base_url=self.chatwoot_url,
account_id=lead.account_id,
access_key=self.access_key)
access_key=self.access_key,
ssl=self.ssl)

is_private = (metadata or {}).get("private", False)

Expand All @@ -215,7 +219,8 @@ async def send_audio_message(self,

client = ChatwootMessages(base_url=self.chatwoot_url,
account_id=lead.account_id,
access_key=self.access_key)
access_key=self.access_key,
ssl=self.ssl)

is_private = (metadata or {}).get("private", False)
attach = ChatwootAttachment(type="audio",
Expand Down Expand Up @@ -258,7 +263,8 @@ async def update_bot():
client = ChatwootAgentsBots(
base_url=self.chatwoot_url,
account_id=self.account_id,
access_key=self.access_key
access_key=self.access_key,
ssl=self.ssl
)

bot = await client.upsert_bot(name=self.bot_name,
Expand Down

0 comments on commit d8f6f57

Please sign in to comment.