-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteamupbot.py
116 lines (101 loc) · 4.34 KB
/
teamupbot.py
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
#!/usr/bin/python
# coding: utf8
import requests
import datetime
import calendar
from dateutil import parser
from telegram.ext import Updater
import logging
from conf import *
def tu_getchanges(bot, job):
tu_headers = {'Teamup-Token': TEAMUP_APIKEY}
tu_url = TEAMUP_APIURL + '/' + TEAMUP_APICALENDAR + '/events'
dt_lastcall = datetime.datetime.utcnow() + datetime.timedelta(minutes=-INTERVAL_MINUTES)
tu_lastcall = calendar.timegm(dt_lastcall.timetuple())
tu_payload = {'modifiedSince': tu_lastcall}
tu_response = requests.get(tu_url, headers=tu_headers, params=tu_payload)
tu_changedevents = tu_response.json()
data_posttg = []
for tu_event in tu_changedevents['events']:
tu_startdate = parser.parse(tu_event['start_dt'])
tu_enddate = parser.parse(tu_event['end_dt'])
txt_post = tu_event['title'] + " - " + TEAMUP_APISUBCALENDAR[str(tu_event['subcalendar_id'])] + " - " + tu_startdate.strftime('%d.%m.%Y %H') + "-" + tu_enddate.strftime('%H h')
if (tu_event['delete_dt'] is not None):
txt_post += " geloescht"
data_posttg.append(txt_post)
msg = "Kalenderupdate:\n"
if len(data_posttg) < 20 and len(data_posttg) > 0:
for ev in data_posttg:
msg += ev + "\n"
msg += "\n"
msg += "https://teamup.com/"+TEAMUP_APICALENDAR+"\n"
for chat in Chats:
print(chat)
bot.send_message(chat_id=chat,
text=msg)
elif len(data_posttg) > 0 :
msg += "Zuviele Änderungen..."
msg += "\n"
msg += "https://teamup.com/"+TEAMUP_APICALENDAR+"\n"
for chat in Chats:
bot.send_message(chat_id=chat,
text=msg)
#bot.send_message(chat_id='94397642',
# text=msg)
def tu_gettoday(bot, update):
tu_headers = {'Teamup-Token': TEAMUP_APIKEY}
tu_url = TEAMUP_APIURL + '/' + TEAMUP_APICALENDAR + '/events'
dt_start = datetime.date.today()
#dt_end = dt_start + datetime.timedelta(hours=24)
tu_start = datetime.date.isoformat(dt_start)
#tu_end = datetime.date.isoformat(dt_end)
tu_payload = {'startDate': tu_start, 'endDate': tu_start}
#tu_payload = {}
tu_response = requests.get(tu_url, headers=tu_headers, params=tu_payload)
tu_todayevents = tu_response.json()
data_posttg = []
for tu_event in tu_todayevents['events']:
tu_startdate = parser.parse(tu_event['start_dt'])
tu_enddate = parser.parse(tu_event['end_dt'])
txt_post = tu_event['title'] + " - " + TEAMUP_APISUBCALENDAR[str(tu_event['subcalendar_id'])] + " - " + tu_startdate.strftime('%d.%m.%Y %H') + "-" + tu_enddate.strftime('%H h')
data_posttg.append(txt_post)
msg = "Heute:\n"
if len(data_posttg) < 20 and len(data_posttg) > 0:
for ev in data_posttg:
msg += ev + "\n"
msg += "\n"
msg += "https://teamup.com/"+TEAMUP_APICALENDAR+"\n"
bot.send_message(chat_id=update.message.chat_id, text=msg)
elif len(data_posttg) > 0 :
msg += "Zuviele Termine\n"
msg += "\n"
msg += "https://teamup.com/"+TEAMUP_APICALENDAR+"\n"
bot.send_message(chat_id=update.message.chat_id, text=msg)
else:
msg += "Scheint frei zu sein...\n"
msg += "\n"
msg += "https://teamup.com/"+TEAMUP_APICALENDAR+"\n"
bot.send_message(chat_id=update.message.chat_id, text=msg)
#bot.send_message(chat_id='94397642',
# text=msg)
Chats = []
updater = Updater(token=TGRMBOT_TOKEN)
dispatcher = updater.dispatcher
j = updater.job_queue
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="Kalenderupdate-Bot")
Chats.append(update.message.chat_id)
from telegram.ext import CommandHandler
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
today_handler = CommandHandler('today', tu_gettoday)
dispatcher.add_handler(today_handler)
def callback_minute(bot, job):
bot.send_message(chat_id=bot.get_updates()[-1].message.chat_id,
text='One message every minute')
#enable for broadcasting calendar changes to all connected channels
#job_minute = j.run_repeating(tu_getchanges, interval=60*INTERVAL_MINUTES, first=0)
updater.start_polling()
updater.idle()