-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiCamGPIOTelegram.py
170 lines (141 loc) · 3.97 KB
/
PiCamGPIOTelegram.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
# -*- coding: utf-8 -*-
#!/usr/bin/python
import subprocess
import os
import picamera
import time
import shlex
from datetime import datetime
from datetime import timedelta
import datetime as dt
import tgl
import sys
import RPi.GPIO as GPIO
from functools import partial
ppth = os.path.abspath(__file__)
print ppth
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(27,GPIO.OUT)
#
our_id = 0
binlog_done = False;
enable_bot = False
#strup_msg = False
def on_binlog_replay_end():
binlog_done = True;
def on_get_difference_end():
pass
def on_our_id(id):
our_id = id
return "Set ID: " + str(our_id)
def msg_cb(success, msg):
pass
#pp.pprint(success)
#pp.pprint(msg)
HISTORY_QUERY_SIZE = 100
def history_cb(msg_list, peer, success, msgs):
print(len(msgs))
msg_list.extend(msgs)
print(len(msg_list))
if len(msgs) == HISTORY_QUERY_SIZE:
tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
def cb(success):
print(success)
print("\ncb function")
def on_msg_receive(msg):
if msg.out and not binlog_done:
return;
global enable_bot
peer = msg.src
mystr = msg.text.lower()
w = mystr.split()
if (msg.src.id == 97329959):#ADMIN ID
if (mystr =='dison'):
enable_bot = True
peer.send_msg ('Program is Disable.')
return;
elif (mystr =='disoff'):
enable_bot = False
peer.send_msg ('Program is Enable.')
return;
elif (mystr == 'reboot'):
peer.send_msg ('Server is Reboot.')
cmd = 'sudo reboot'
subprocess.call(cmd.split())
return;
elif (mystr == 'shutdown'):
peer.send_msg ('Server is Shutdown.')
cmd = 'sudo shutdown -h now'
subprocess.call(cmd.split())
return;
elif (w[0] == 'led') and (not(w[1] == None)):
if (w[1]=='off'):
peer.send_msg ('Led turned off.')
GPIO.output(27,0)
return;
elif (w[1]=='on'):
peer.send_msg ('Led turned on.')
GPIO.output(27,1)
return;
else:
peer.send_msg ('Unknown command.')
return;
if (not(enable_bot)):
if (mystr=='about'):
peer.send_msg ('Telegram bot v0.1b\n\nProgrammer: @RaminSangesari\nEmail: r_sangsari@yahoo.ca')
return;
elif (mystr == '!photo'):
path=os.getenv("HOME")
with picamera.PiCamera() as picam:
#picam.led = False
#picam.rotation=90
picam.framerate = 24
picam.start_preview()
picam.annotate_background = picamera.Color('black')
picam.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
picam.capture(path+'/pic.jpg',resize=(640,480))
time.sleep(2)
picam.stop_preview()
picam.close()
print(path)
peer.send_photo ('/root/pic.jpg')
peer.send_msg ('Send the picture may take some time.\nPlease wait...')
return;
elif (mystr == 'id'):
iid = 'Your telegram id : '+str(msg.src.id)
peer.send_msg(iid)
return;
elif (mystr == 'time'):
ttime=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
peer.send_msg (ttime)
return;
elif (mystr=='uptime'):
with open('/proc/uptime', 'r') as f:
usec = float(f.readline().split()[0])
usec_str = str(timedelta(seconds = usec))
peer.send_msg(usec_str)
return;
elif (mystr == 'help') or (mystr == '?'):
peer.send_msg ('Commands List:\n\nid\nabout\nuptime\nphoto\ntime\n\nCommands is not case-sensitive.')
return;
else:
peer.send_msg ('For more information please send help or ?\nCommands is not case-sensitive.')
peer.send_msg (msg.src)
return;
def print_path(success, filename):
print(filename)
def on_secret_chat_update(peer, types):
return "on_secret_chat_update"
def on_user_update(peer, what_changed):
pass
def on_chat_update(peer, what_changed):
pass
# Set callbacks
tgl.set_on_binlog_replay_end(on_binlog_replay_end)
tgl.set_on_get_difference_end(on_get_difference_end)
tgl.set_on_our_id(on_our_id)
tgl.set_on_secret_chat_update(on_secret_chat_update)
tgl.set_on_user_update(on_user_update)
tgl.set_on_chat_update(on_chat_update)
tgl.set_on_msg_receive(on_msg_receive)