Skip to content

Instantly share code, notes, and snippets.



Last active Jan 9, 2020
What would you like to do?
token = secrettoken
url =
user = secretusertoken
app = secretapptoken
import configparser
import json
from contextlib import suppress
import requests
config = configparser.ConfigParser()"notify.cfg")
def get_seen_ids():
with suppress(FileNotFoundError):
with open("./seen_ids") as seen_file:
content =
return set(int(element) for element in content.strip().split("\n") if element)
return set()
def write_seen_ids(ids):
content = "\n".join(str(element) for element in ids)
with open("./seen_ids", "w") as seen_file:
def zammad_get(url):
response = requests.get(
url=config["zammad"]["url"] + url,
headers={"Authorization": f'Bearer {config["zammad"]["token"]}'},
return response.json()
def get_unread_notifications():
response = zammad_get("/api/v1/online_notifications")
return [notification for notification in response if notification["seen"] is False]
def limit_length(text, length):
if len(text) <= length:
return text
return text[: length - 2] + "…"
def send_notification(notification):
ticket_id = notification["o_id"]
ticket = zammad_get(f"/api/v1/ticket_articles/by_ticket/{ticket_id}")[0]
if any(ticket.get(text, "").startswith("RT @") for text in ["subject", "body"]):
ticket_data = zammad_get(f"/api/v1/tickets/{ticket_id}")
customer = zammad_get(f'/api/v1/users/{ticket_data["customer_id"]}')
name = customer.get("firstname", " ") + " " + customer.get("lastname", "")
name = name.strip()
if name:
name += f'<{customer["email"]}>'
name = customer["email"]
payload = {
"token": config["pushover"]["app"],
"user": config["pushover"]["user"],
"title": limit_length(f"{ticket['subject']} ({name})", 250),
"message": limit_length(ticket["body"], 1024),
"url": config["zammad"]["url"] + f'/#ticket/zoom/{ticket_id}',
"url_title": "Go to ticket",
"sound": "none",
response =
headers={"Content-Type": "application/json"},
def main():
seen_ids = get_seen_ids()
notifications = get_unread_notifications()
new_seen_ids = []
for notification in notifications:
if notification["id"] not in seen_ids:
if __name__ == "__main__":

This comment has been minimized.

Copy link

@wauwturrr wauwturrr commented Jan 9, 2020

Hi Rixx,

When i'm testing the script, i'm getting the error below:

root@vmi333784:/zammadnotify# ./
File "./", line 29
headers={"Authorization": f'Bearer {config["zammad"]["token"]}'},
SyntaxError: invalid syntax

Do you know how to solve this?


This comment has been minimized.

Copy link
Owner Author

@rixx rixx commented Jan 9, 2020

This script requires Python 3.6 and above, you seem to be running on a lower version. You'll have to replace every f-string (a string that starts with f' or f") with an old-style format string or string concatenation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.