Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to create new labels in account #120

Open
psresnik opened this issue Aug 26, 2024 · 1 comment
Open

Adding ability to create new labels in account #120

psresnik opened this issue Aug 26, 2024 · 1 comment

Comments

@psresnik
Copy link

The add_label routine will fail if the label doesn't already exist for the account. Can a create_label_in_account functionality be added? E.g. something along the lines of the following? (I've thought about forking the repo and doing this myself, but I don't have a lot of experience doing that so I thought I'd start with a feature request, thanks...)

    # Define the new label's properties
    label_body = {
        "labelListVisibility": "labelShow",  # Set label visibility in the label list
        "messageListVisibility": "show",     # Set label visibility in the message list
        "name": new_label                     # Set the name of the new label
    }

    try:
        # Create the label
        label = service.users().labels().create(userId='me', body=label_body).execute()
        print(f"Label '{label['name']}' created successfully.")
        return label

    except googleapiclient.errors.HttpError as error:
        print(f"An error occurred: {error}")
        return None
@psresnik
Copy link
Author

Never mind! The following worked:

def create_label(label):
    gmail = Gmail()
    labels = gmail.list_labels()
    if label not in [label.name for label in labels]:
        print(f"Adding new label {label} to account")
        gmail.create_label(label)
    else:
        print(f"Verified that label {label} exists in account")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant