Chris4K/push-notification-tool
0
1from exponent_server_sdk import (2 DeviceNotRegisteredError,3 PushClient,4 PushMessage,5 PushServerError,6 PushTicketError,7)8import os9 10from transformers.tools.base import Tool11 12# Description for the tool13PUSH_NOTIFICATION_DESCRIPTION = (14 "This is a tool that sends a push notification using Expo. It takes two inputs: "15 "`token`, which is the Expo push token of the recipient, and `message`, which is the "16 "message to send. It does not return any output."17)18 19class PushNotificationTool(Tool):20 description = PUSH_NOTIFICATION_DESCRIPTION21 name = "push_notification"22 inputs = ["text", "text"]23 outputs = []24 25 def __call__(self, token:str, message:str):26 try:27 # Replace 'your_expo_token' with your actual Expo push notification token28 response = PushClient().publish(29 PushMessage(to=token, body=message)30 )31 except PushServerError as exc:32 # Encountered some likely formatting/validation error.33 print(f"Error: {exc.errors}")34 raise35 except DeviceNotRegisteredError:36 # Mark the push token as inactive37 print("Device not registered.")38 # Add logic to handle inactive push token in your application39 except PushTicketError as exc:40 # Encountered some other per-notification error.41 print(f"Push ticket error: {exc.push_response._asdict()}")42 raise43 44 try:45 # We got a response back, but we don't know whether it's an error yet.46 # This call raises errors so we can handle them with normal exception flows.47 response.validate_response()48 except DeviceNotRegisteredError:49 # Mark the push token as inactive50 print("Device not registered.")51 # Add logic to handle inactive push token in your application52 except PushTicketError as exc:53 # Encountered some other per-notification error.54 print(f"Push ticket error: {exc.push_response._asdict()}")55 raise56 57 return None # No output is expected58 