-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoicer.py
More file actions
139 lines (111 loc) · 3.59 KB
/
Copy pathinvoicer.py
File metadata and controls
139 lines (111 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import json
import os
from typing import Callable
import dynamics as dyn
from inserter import LatexBuilder
from datetime import datetime
from helper import Datafile, get_event_data, strip, InputError
from dotenv import load_dotenv
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt import App
from json_blocks import __path__
load_dotenv()
app = App(token=os.environ["TOKEN"],signing_secret=os.environ["SIGNING_SECRET"])
# Declare the event data dict which will be filled once the home-tab
# of the bot is opened
EVENT_DATA: dict
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
global EVENT_DATA
# Parse the home_tab_view json file
with open(__path__[0] + "/home_tab_view.json", "r") as file:
home = json.load(file)
try:
client.views_publish(
user_id=event["user"],
view = json.dumps(home)
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
# Pull the event data from the endpoint
EVENT_DATA = get_event_data()
return
# Say something upon mentioning the bot
@app.event("app_mention")
def event_test(say):
say("What's up?")
@app.message("")
def message_hello(message, say):
user = message["user"]
say(text=f"Hey there <@{user}>!")
# Open the invoice modal via its global shortcut
@app.global_shortcut("open-invoice-modal")
def open_invoice_modal(ack, shortcut, client):
ack()
client.views_open(
trigger_id=shortcut["trigger_id"],
# A simple view payload for a modal
view = dyn.prepare_modal(
dyn.create_event_list(
get_event_data()
)
)
)
# Open the invoice modal but this time by pressing the corresponding button
# in the home tab
@app.action("home-submit-button")
def open_invoice_modal_from_home(ack, body, client):
ack()
# Open the modal
client.views_open(
trigger_id = body["trigger_id"],
view = dyn.prepare_modal(
dyn.create_event_list(
get_event_data()
)
)
)
@app.action("event-selection")
def handle_selection(ack, body):
ack()
@app.action("users_select-action")
def handle_some_action(ack, body):
ack()
@app.action("invoice-date-select")
def handle_datepicker(ack, body):
invoice_date = body["actions"][0]["selected_date"]
# Check if selected date lies in the future
today = datetime.today()
to_check = datetime.strptime(invoice_date,"%Y-%m-%d")
if today < to_check:
#raise InputError("Du kannst keine Rechnungen aus der "
# "Zukunft einreichen.", "event_date")
errors = {}
errors["invoice-date-select"] = ("Du kannst keine Rechnungen aus der "
"Zukunft einreichen.")
ack(response_action="errors",errors=errors)
return
else:
ack()
return
@app.view("invoice")
def handle_view_events(ack, body, client):
values: dict = body["view"]["state"]["values"]
user: str = body["user"]["id"]
try:
stripped: dict = strip(values, user)
except InputError as e:
errors = {}
errors[e.args[1]] = e.args[0]
ack(response_action="errors",errors=errors)
return
ack()
file = Datafile(stripped["event_name"])
file.store(stripped)
builder = LatexBuilder(file.filename,file.event_date)
builder.set()
builder.compile()
return
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SOCKET_TOKEN"]).start()