-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_button.py
85 lines (62 loc) · 1.81 KB
/
push_button.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
# helpers
import time
from pprint import pprint
# telepot
import telepot
from telepot.delegate import pave_event_space, per_chat_id, create_open
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove, ForceReply
my_chat_id = -1
#
# Push Button
#
from gpio_classes import PushButton
from time import sleep
import datetime
class BotButton(PushButton):
def __init__(self, name, bx):
PushButton.__init__(self, name)
self.bot = bx
def pressed(self):
global my_chat_id
print
">>>> Button pressed"
if my_chat_id >= 0:
self.bot.sendMessage(my_chat_id, 'BUTTON PRESSED at %s' % datetime.datetime.utcnow())
else:
print("error: no chat open")
def released(self):
print
">>>> Button released"
#
# main Bot class
#
class PushButtonBot(telepot.helper.ChatHandler):
def __init__(self, *args, **kwargs):
super(PushButtonBot, self).__init__(*args, **kwargs)
self._count = 0
def on_chat_message(self, msg):
global my_chat_id
pprint(msg)
my_chat_id = msg['chat']['id']
command = msg['text']
self._count += 1
self.sender.sendMessage("count: %d chat id: %s command: %s"
%
(self._count, my_chat_id, command))
#
# register the bot and start it
#
import config
# Il token è contenuto nel file config.py e non è aggiornato su GitHub per evitare utilizzi impropri
TOKEN=config.TOKEN
bot = telepot.DelegatorBot(os.environ['SSH_BOT_KEY'], [
pave_event_space()(
per_chat_id(), create_open, PushButtonBot, timeout=1000
),
])
bot.message_loop(run_forever=False, timeout=2)
print
"Message loop started"
button = BotButton('PC17', bot)
while True:
time.sleep(100000)