This commit is contained in:
asyncnomi 2023-02-16 23:43:14 +01:00
parent 6361e5f038
commit 8187ed156e
5 changed files with 130 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__/

88
src/main.py Normal file
View file

@ -0,0 +1,88 @@
"""""""""""""""""""""""
Zammad <-> Telegram bot
"""""""""""""""""""""""
from telegram_interface import *
from zammad_interface import *
import threading
import json
"""
Constant definition
"""
# Refresh frequency in second
refresh_delay = 2
"""
Funciton definition
"""
# Save the tickets in used to a file
def save_json(dict):
fs = open("../tickets.txt", "w")
fs.write(json.dumps(dict))
fs.close()
# Fetch from the same file the tickets
def load_json():
fs = open("../tickets.txt", "r")
stringify = fs.read()
fs.close()
parsed = json.loads(stringify)
return parsed
# Close btn event handler
def close_btn_handler(telegram_id):
ticket_id = [ticket_id for ticket_id in tickets if tickets[ticket_id]["telegram_id"] == telegram_id][0]
switch_to_close(ticket_id)
tickets.pop(ticket_id)
save_json()
# looper
def set_interval(func,time):
e = threading.Event()
while not e.wait(time):
func()
# main loop
def main():
# Fetch tickets from zammad
current_tickets = fetch_tickets()
# Refresh tickets on telegram if any are new
for ticket in current_tickets:
if ticket["id"] not in tickets:
# Add the new ticket and push a message to telegram
tickets[ticket["id"]] = {
"telegram_id": None,
"zammad_ticket": ticket,
}
telegram_id = push_new_ticket(ticket)
tickets[ticket["id"]]["telegram_id"] = telegram_id
save_json()
else:
# Check for any update
if json.dumps(tickets[ticket["id"]]["zammad_ticket"]) != json.dumps(ticket):
update_ticket(tickets[ticket["id"]]["telegram_id"], ticket)
# Switch telegram msg of a ticket to close if it no longer appears among the open tickets on zammad
for ticket_id in tickets:
if ticket_id not in [ticket["id"] for ticket in current_tickets]:
switch_to_close(tickets[ticket_id]["telegram_id"])
tickets.pop(ticket_id)
save_json()
"""
The damn thing
"""
# Fetch previously in used tickets
tickets = load_json()
# Wait for both side to be ready
init_zammad()
init_telegram()
# Run the damn thing
set_interval(main, refresh_delay)

28
src/telegram_interface.py Normal file
View file

@ -0,0 +1,28 @@
"""""""""""""""""""""
Telegram interface
"""""""""""""""""""""
# Anything that need to be run before the main loop is started
def init_telegram():
pass
# Push a new message to the telegram channel with the relevant information extracted from ticket
# Return the id of the telegram freshly created message
def push_new_ticket(ticket):
pass
# Update a ticket from the associated message id with the new updated ticket
def update_ticket(msg_id, updated_ticket):
pass
# Switch a message to the closed state from its id
def switch_to_close(msg_id):
pass
# Event subscribers declaration
# on close btn press event <- replace that with what it describes
def close_btn_callback(something):
msg_id = "the mesage id fetch from something"
close_btn_handler(msg_id)
# I need to find a way to make a jump to this function in the main file
# Can't just import the file unfortunately

12
src/zammad_interface.py Normal file
View file

@ -0,0 +1,12 @@
"""""""""""""""""""""
Zammad interface
"""""""""""""""""""""
# Anything that need to be run before the main loop is started
def init_zammad():
pass
# Fetch all tickets with the open state
# And return the following: [{all the ticket thing return by the api},...]
def fetch_tickets():
pass

1
tickets.txt Normal file
View file

@ -0,0 +1 @@
{}