This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
172 lines (145 loc) · 3.97 KB
/
sample.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from twx.botapi import TelegramBot
import r3door
import r3temp
import config
import json
def setupBot(apitoken):
"""
Setup the bot
"""
bot = TelegramBot(apitoken)
bot.update_bot_info().wait()
return bot
def saveBotState(state):
"""
Saves the bot's state to state.json file.
"""
with open('state.json', 'w') as fp:
json.dump(state, fp)
def loadBotState():
"""
Loads the bot's state from state.json file.
"""
try:
with open('state.json', 'r') as fp:
state = json.load(fp)
except:
state = dict()
state['offset'] = 0
return state
def unsetWebhook(bot):
"""
Since we will not be able to receive updates
using getUpdates for as long as an outgoing
webhook is set up, we need to unset it here.
"""
return bot.set_webhook(url=None)
def getTemperatureString():
"""
Retrieve temperature data
using the r3 Space-API.
"""
api = r3temp.r3temp()
return '''
Temperatures in r3:
Temp Outside: %s°C
Temp in LoTHR: %s°C
Temp in CX: %s°C
Temp in OLGA Room: %s°C
Temp in OLGA freezer: %s°C
Temp in UPS Battery: %s°C
''' % (
api.getTempByName('Temp@Outside'),
api.getTempByName('Temp@LoTHR'),
api.getTempByName('Temp@CX'),
api.getTempByName('Temp@OLGA Room'),
api.getTempByName('Temp@OLGA freezer'),
api.getTempByName('Temp@UPS Yellow Battery')
)
def getDoorstatusString():
"""
Retrieve door status data
using the r3 Space-API.
"""
api = r3door.r3door()
status = api.getDoorstatus()
return '''
Doorstatus of r3 frontdoor:
locked: %s
kontakted: %s
''' % (
status[0], status[1]
)
def match(text, words):
"""
Returns True if any of 'words' is contained
in 'text', otherwise returns False.
"""
return any(x in text for x in words)
strings_door = ['door', 'tuer', u'tür', 'status']
strings_temp = ['temp', 'temperature', 'temperatur', 'status']
def getReplyForMessage(msg, sender):
"""
Does the magic. Returns a string.
"""
msgLower = msg.lower()
if match(msgLower, strings_door):
return getDoorstatusString()
elif match(msgLower, strings_temp):
return getTemperatureString()
else:
return 'Sorry, %s. I don\'t understand that (so far?) ...' % (sender)
def getReplyForUpdate(update):
"""
Hands the given update to getReplyForMessage()
and logs it using logupdate().
Returns the returned string.
"""
logUpdate(update)
return getReplyForMessage(
update.message.text,
update.message.sender.first_name)
def logUpdate(update):
"""
Does a quick log of the received message
to stdout.
"""
print '%s: %s' % (
update.message.sender.first_name, update.message.text)
def updateLoop(bot, state):
"""
Retrieves updates in an infinite loop.
Hands received update to getReplyForUpdate()
and sends the returned string to the user
via Telegram.
"""
running = True
while running:
updates = bot.get_updates(offset=state['offset']).wait()
for update in updates:
state['offset'] = update.update_id + 1
reply = getReplyForUpdate(update)
bot.send_message(
update.message.sender.id,
reply).wait()
if __name__ == '__main__':
"""
main code.
runs this script in an (infinite) loop,
until a KeyboardInterrupt is caught.
"""
bot = setupBot(config.APITOKEN)
state = loadBotState()
print '> %s ready to rock! offset = %d' % (
bot.username, state['offset'])
if unsetWebhook(bot):
print '> successfully unset webhook.'
try:
updateLoop(bot, state)
except KeyboardInterrupt:
pass
print 'shutting down bot ... ',
saveBotState(state)
print 'done!'