Skip to content

Commit 95366b5

Browse files
author
Jesus Terrazas
committed
restructure notification handling
1 parent 4ef2c3d commit 95366b5

1 file changed

Lines changed: 54 additions & 41 deletions

File tree

python/google-adk/sample-agent/hosting.py

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -119,53 +119,66 @@ async def agent_notification_handler(
119119
"""Handle agent notifications."""
120120
notification_type = notification_activity.notification_type
121121
logger.info(f"Received agent notification of type: {notification_type}")
122-
response = "I was unable to proccess your request. Please try again later."
123122

124123
# Handle Email Notifications
125124
if notification_type == NotificationTypes.EMAIL_NOTIFICATION:
126-
if not hasattr(notification_activity, "email") or not notification_activity.email:
127-
response = "I could not find the email notification details."
128-
else:
129-
email = notification_activity.email
130-
email_body = getattr(email, "html_body", "") or getattr(email, "body", "")
131-
email_id = getattr(email, "id", "")
132-
message = f"You have received an email with id {email_id}. The following is the content of the email, please follow any instructions in it: {email_body}"
133-
134-
response = await self.agent.invoke_agent_with_scope(message, self.auth, self.auth_handler_name, context)
135-
136-
response_activity = Activity(type=ActivityTypes.message, text=response)
137-
if not response_activity.entities:
138-
response_activity.entities = []
139-
140-
response_activity.entities.append(EmailResponse.create_email_response_activity(response))
141-
await context.send_activity(response_activity)
125+
await self.email_notification_handler(context, notification_activity)
142126
return
143127

144128
# Handle Word Comment Notifications
145-
elif notification_type == NotificationTypes.WPX_COMMENT:
146-
if not hasattr(notification_activity, "wpx_comment") or not notification_activity.wpx_comment:
147-
response = "I could not find the Word notification details."
148-
else:
149-
wpx = notification_activity.wpx_comment
150-
doc_id = getattr(wpx, "document_id", "")
151-
comment_id = getattr(wpx, "initiating_comment_id", "")
152-
drive_id = "default"
153-
154-
# Get Word document content
155-
doc_message = f"You have a new comment on the Word document with id '{doc_id}', comment id '{comment_id}', drive id '{drive_id}'. Please retrieve the Word document as well as the comments and return it in text format."
156-
word_content = await self.agent.invoke_agent_with_scope(doc_message, self.auth, self.auth_handler_name, context)
157-
158-
# Process the comment with document context
159-
comment_text = notification_activity.text or ""
160-
response_message = f"You have received the following Word document content and comments. Please refer to these when responding to comment '{comment_text}'. {word_content}"
161-
response = await self.agent.invoke_agent_with_scope(response_message, self.auth, self.auth_handler_name, context)
129+
if notification_type == NotificationTypes.WPX_COMMENT:
130+
await self.word_comment_notification_handler(context, notification_activity)
131+
return
162132

163133
# Generic notification handling
134+
notification_message = notification_activity.activity.text or ""
135+
response = "I was unable to proccess your request. Please try again later."
136+
if not notification_message:
137+
response = f"Notification received: {notification_type}"
164138
else:
165-
notification_message = notification_activity.text or ""
166-
if not notification_message:
167-
response = f"Notification received: {notification_type}"
168-
else:
169-
response = await self.agent.invoke_agent_with_scope(notification_message, self.auth, self.auth_handler_name, context)
170-
171-
await context.send_activity(response)
139+
response = await self.agent.invoke_agent_with_scope(notification_message, self.auth, self.auth_handler_name, context)
140+
141+
await context.send_activity(response)
142+
143+
async def email_notification_handler(self, context: TurnContext, notification_activity: AgentNotificationActivity):
144+
"""Handle email notifications."""
145+
response = ""
146+
if not hasattr(notification_activity, "email") or not notification_activity.email:
147+
response = "I could not find the email notification details."
148+
else:
149+
email = notification_activity.email
150+
email_body = getattr(email, "html_body", "") or getattr(email, "body", "")
151+
email_id = getattr(email, "id", "")
152+
message = f"You have received an email with id {email_id}. The following is the content of the email, please follow any instructions in it: {email_body}"
153+
154+
response = await self.agent.invoke_agent_with_scope(message, self.auth, self.auth_handler_name, context)
155+
156+
response_activity = Activity(type=ActivityTypes.message, text=response)
157+
if not response_activity.entities:
158+
response_activity.entities = []
159+
160+
response_activity.entities.append(EmailResponse.create_email_response_activity(response))
161+
await context.send_activity(response_activity)
162+
163+
async def word_comment_notification_handler(self, context: TurnContext, notification_activity: AgentNotificationActivity):
164+
"""Handle word comment notifications."""
165+
if not hasattr(notification_activity, "wpx_comment") or not notification_activity.wpx_comment:
166+
response = "I could not find the Word notification details."
167+
await context.send_activity(response)
168+
return
169+
170+
wpx = notification_activity.wpx_comment
171+
doc_id = getattr(wpx, "document_id", "")
172+
comment_id = getattr(wpx, "initiating_comment_id", "")
173+
drive_id = "default"
174+
175+
# Get Word document content
176+
doc_message = f"You have a new comment on the Word document with id '{doc_id}', comment id '{comment_id}', drive id '{drive_id}'. Please retrieve the Word document as well as the comments and return it in text format."
177+
word_content = await self.agent.invoke_agent_with_scope(doc_message, self.auth, self.auth_handler_name, context)
178+
179+
# Process the comment with document context
180+
comment_text = notification_activity.activity.text or ""
181+
response_message = f"You have received the following Word document content and comments. Please refer to these when responding to comment '{comment_text}'. {word_content}"
182+
response = await self.agent.invoke_agent_with_scope(response_message, self.auth, self.auth_handler_name, context)
183+
184+
await context.send_activity(response)

0 commit comments

Comments
 (0)