rare-sapphire
rare-sapphire3mo ago

[Solved ✅] custom action not working

I am trying to create a custom action in gmail to create a draft reply in email thread, below is my code @action(toolname="gmail", requires=["requests"]) def create_gmail_draft_reply(auth: dict, custom_param: dict) -> str: """ Create a draft reply in a Gmail thread using Gmail API. :param auth: Dictionary containing authentication headers and base URL :param custom_param: Dictionary with 'threadId', 'messageId', 'to', 'from', 'subject', and 'body' :return info: String response from the Gmail API """ # Fetch authentication headers headers = auth["headers"] base_url = auth["base_url"] # Extract custom parameters thread_id = custom_param.get("threadId") message_id = custom_param.get("messageId") to_email = custom_param.get("to") from_email = custom_param.get("from") subject = custom_param.get("subject") body_text = custom_param.get("body") # Validate required parameters if not all([thread_id, message_id, to_email, from_email, subject, body_text]): return "Missing required parameters in custom_param" message = MIMEText(body_text) message['to'] = to_email message['from'] = from_email message['subject'] = subject message['In-Reply-To'] = message_id message['References'] = message_id raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode() draft_body = { 'message': { 'raw': raw_message, 'threadId': thread_id } } draft_url = f"{base_url}/users/me/drafts" response = requests.post(draft_url, headers=headers, json=draft_body) return str(response.json()) custom_param = { "threadId": "XX", "messageId": "XX", "to": "XX", "from": "me", "subject": "Re: Subject", "body": "This is a reply to your email." } entity = composio_toolset.get_entity(id='ID') res = entity.execute( action=create_gmail_draft_reply, params=custom_param )
No description
3 Replies
flat-fuchsia
flat-fuchsia3mo ago
hey i will look into this error, however, i think we already have a create email draft action.
rare-sapphire
rare-sapphireOP3mo ago
create email draft is there but creating draft in thread is not there
Soham
Soham3mo ago
This hsas been resolved. The code is following
import typing as t
from composio_crewai import ComposioToolSet, action, Action
from pydantic import BaseModel
import base64

from dotenv import load_dotenv

load_dotenv()

toolset = ComposioToolSet()


@action(toolname="gmail")
def create_draft_reply(
thread_id: str,
message_body: str,
execute_request: t.Callable,
user_id: str = "me",
) -> dict:
"""
Create a draft reply to a specific Gmail thread.

:param thread_id: The ID of the thread to which the reply is to be drafted.
:param message_body: The content of the draft reply.
:param execute_request: A callable function to execute the API request.
:param user_id: The user's email address. Default is 'me' for the authenticated user.
:return draft: The created draft details.
"""
request_body = {
"message": {
"threadId": thread_id,
"raw": base64.urlsafe_b64encode(
f"Content-Type: text/plain; charset=UTF-8\n\n{message_body}".encode(
"utf-8"
)
).decode("utf-8"),
}
}

response = execute_request(
f"/gmail/v1/users/{user_id}/drafts", "post", request_body, None
)
return response.get("data", {})


response = toolset.execute_action(
action=create_draft_reply, params={"thread_id": "1939c189dce2a30c", "message_body": "Hello, how are you?"}
)
print("\nResponse: ", response)
import typing as t
from composio_crewai import ComposioToolSet, action, Action
from pydantic import BaseModel
import base64

from dotenv import load_dotenv

load_dotenv()

toolset = ComposioToolSet()


@action(toolname="gmail")
def create_draft_reply(
thread_id: str,
message_body: str,
execute_request: t.Callable,
user_id: str = "me",
) -> dict:
"""
Create a draft reply to a specific Gmail thread.

:param thread_id: The ID of the thread to which the reply is to be drafted.
:param message_body: The content of the draft reply.
:param execute_request: A callable function to execute the API request.
:param user_id: The user's email address. Default is 'me' for the authenticated user.
:return draft: The created draft details.
"""
request_body = {
"message": {
"threadId": thread_id,
"raw": base64.urlsafe_b64encode(
f"Content-Type: text/plain; charset=UTF-8\n\n{message_body}".encode(
"utf-8"
)
).decode("utf-8"),
}
}

response = execute_request(
f"/gmail/v1/users/{user_id}/drafts", "post", request_body, None
)
return response.get("data", {})


response = toolset.execute_action(
action=create_draft_reply, params={"thread_id": "1939c189dce2a30c", "message_body": "Hello, how are you?"}
)
print("\nResponse: ", response)

Did you find this page helpful?