"""
Core functionality for Simple-Chatwoot
"""
from typing import Dict
import json
import requests
from .utils import check_response
[docs]class ChatWoot:
"""
Initialize the instance with the given parameters.
Arguments
* param api_access_token -- should be an administrators security token for the account, example: rkoo0op2PPsihsv8JW3IjfiF
* param account_id -- numeric ID of the account, example: 1
* param inbox_id -- ID of the inbox, example: 1
* param domain -- domain to use for connecting to Chatwoot instance, example: https://chatwoot.example.com
"""
def __init__(self,
domain:str,
api_access_token:str,
account_id:str,
inbox_id:str,
) -> None:
self.domain = domain
self.api_access_token = api_access_token
self.account_id = account_id
self.inbox_id = inbox_id
def __repr__(self) -> str:
return "ChatWoot Client for account {} & inbox {}".format(self.account_id, self.inbox_id)
### AGENT ###
[docs] def create_agent(self):
"""
Add a new Agent to Account
# Not implemented yet. Here for documentation purposes.
"""
pass
### CONTACT ###
### CONVERSATION ###
[docs] def create_conversation(self,
contact_source_id:str,
contact_id:str=None,
assignee_id:str=None,
team_id:str=None,
additional_attributes:Dict={ },
status:str="open",
**kargs
)->str:
"""
Creating a conversation in chatwoot requires a source id.
Learn more about source_id: https://github.com/chatwoot/chatwoot/wiki/Building-on-Top-of-Chatwoot:-Importing-Existing-Contacts-and-Creating-Conversations
Arguments
* contact_source_id -- source id could be the identifier hash in case of a webwidget, twitter_id in case of a twitter profile and email in case of email channel, example: 561f3286-a92e-4b59-ae1d-9301154313f1
* contact_id -- contact Id for which conversation is created
* assignee_id -- agent Id for assigning a conversation to an agent
* team_id -- team Id for assigning a conversation to a team
* additional_attributes -- lets you specify attributes like browser information
* status -- specify the conversation whether it's pending, open, closed
* kargs -- additional named arguments
"""
# Note: inbox_id is the Id of inbox in which the conversation is created
# Allowed Inbox Types: Website, Phone, Api, Email
payload = {
'source_id': contact_source_id,
'inbox_id': self.inbox_id,
'contact_id': contact_id,
'additional_attributes': additional_attributes,
'status': status,
'assignee_id':assignee_id,
'team_id':team_id,
}
# If user sends other arguments, update payload
payload.update(dict(kargs))
headers = {'api_access_token': self.api_access_token, 'Content-type': 'application/json'}
response = requests.post(self.domain+"/api/v1/accounts/"+self.account_id+"/conversations",
data=json.dumps(payload),
headers=headers)
json_response_dict = check_response(response, 200, "Conversation Creation Failed")
conversation_id = str(json_response_dict['id'])
return conversation_id
[docs] def get_conversation_details(self, conversation_id:str)->Dict:
"""
Get all details regarding a conversation with all messages in the conversation
Arguments
* conversation_id -- numeric ID of the conversation
"""
headers = {'api_access_token': self.api_access_token, 'Content-type': 'application/json'}
response = requests.get(self.domain+"/api/v1/accounts/"+self.account_id+"/conversations/"+conversation_id,
headers=headers)
json_response_dict = check_response(response, 200, "Get Conversation Details Failed")
return json_response_dict
### MESSAGES ###
[docs] def create_message(self,
conversation_id:str,
content:str,
message_type:str="incoming",
is_private:bool=False,
)->str:
"""
Conversation is a numeric identification like 5670
If using API inbox type, make sure your webhook server is up and running otherwise you'll get errors from it
Arguments
* conversation_id -- numeric ID of the conversation
* content -- content of the message
* message_type -- weather it's "outgoing" or "incoming"
* is_private -- flag to identify if it is a private note
"""
payload = {
'content': content,
'message_type': message_type,
'private': is_private,
}
headers = {'api_access_token': self.api_access_token, 'Content-type': 'application/json'}
response = requests.post(self.domain+"/api/v1/accounts/"+self.account_id+"/conversations/"+conversation_id+"/messages",
data=json.dumps(payload),
headers=headers)
json_response_dict = check_response(response, 200, "Message Creation Failed")
message_id = str(json_response_dict['id'])
return message_id
[docs] def list_messages(self, conversation_id:str)->Dict:
"""
List all messages of a conversation
Arguments
* conversation_id -- numeric ID of the conversation
"""
headers = {'api_access_token': self.api_access_token, 'Content-type': 'application/json'}
response = requests.get(self.domain+"/api/v1/accounts/"+self.account_id+"/conversations/"+conversation_id+"/messages",
headers=headers)
json_response_dict = check_response(response, 200, "List Messages Failed")
return json_response_dict
### INBOX ###
[docs] def list_inboxes(self)->Dict:
"""
List all inboxes available in the current account
"""
headers = {'api_access_token': self.api_access_token, 'Content-type': 'application/json'}
response = requests.get(self.domain+"/api/v1/accounts/"+self.account_id+"/inboxes",
headers=headers)
json_response_dict = check_response(response, 200, "Message Creation Failed")
return json_response_dict