---
# System prepended metadata

title: OperationPaul Demo Triage

---

## Before Demo Triage

- set Urgent label on Urgent emails


```python
def modify_email_labels(service, user_id, message_id, add_labels=None, remove_labels=None):
    # add_labels and remove_labels should be list of labelIds
    body = {'addLabelIds': 'urgent'}
    message = service.users().messages().modify(userId=user_id, id=message_id, body=body).execute()
    print(f"Labels changed for message ID: {message_id}")
    return message
```


```python!
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials

def gmail_authenticate(access_token):
    creds = Credentials(token=access_token)
    service = build('gmail', 'v1', credentials=creds)
    return service

def list_labels(service, user_id='me'):
    response = service.users().labels().list(userId=user_id).execute()
    return response.get('labels', [])

def create_label(service, user_id, label_name):
    label_body = {
        'name': label_name,
        'labelListVisibility': 'labelShow',
        'messageListVisibility': 'show'
    }
    label = service.users().labels().create(userId=user_id, body=label_body).execute()
    print(f"Created label: {label['name']} with ID: {label['id']}")
    return label

def main():
    access_token = 'your-access-token'  # Replace this with your actual access token
    service = gmail_authenticate(access_token)
    labels = list_labels(service)

    # Check if 'Urgent' label already exists
    urgent_label = next((label for label in labels if label['name'].lower() == 'urgent'), None)
    
    if urgent_label is None:
        # Label 'Urgent' does not exist, so create it
        urgent_label = create_label(service, 'me', 'Urgent')
    else:
        print(f"Label 'Urgent' already exists with ID: {urgent_label['id']}")

if __name__ == "__main__":
    main()

```