-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_update.py
More file actions
125 lines (93 loc) · 3.28 KB
/
send_update.py
File metadata and controls
125 lines (93 loc) · 3.28 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
import telegram
import os
import json
import threading
_arquivolock = threading.Lock()
base_path = "~/Documents/telegram_bot/subs/"
def envia(bot,processo,mensagem):
for filename in os.listdir(base_path):
try:
file_path = base_path+filename
_arquivolock.acquire()
file = open(file_path,"r")
infos = json.loads(file.read())
file.close()
_arquivolock.release()
envia = None
if processo in infos["inscritos"]:
primeira = None
try:
infos["mensagens_enviadas"][processo]
primeira = False
except:
primeira = True
if primeira==True:
infos["mensagens_enviadas"][processo] = mensagem
envia = True
elif primeira==False:
if infos["mensagens_enviadas"][processo]==mensagem:
envia = False
else:
infos["mensagens_enviadas"][processo] = mensagem
envia = True
_arquivolock.acquire()
file = open(file_path,"w")
file.write(json.dumps(infos))
file.close()
_arquivolock.release()
if envia:
msg = ""
msg += "`"+processo+":` "+mensagem
# print mensagem
bot.sendMessage(
chat_id=infos["cod"],
text=msg,
parse_mode=telegram.ParseMode.MARKDOWN)
except Exception as e:
a = open(base_path+"erros","a")
a.write(mensagem+"\n")
a.write(str(e)+"\n")
a.close()
raise
def enviaImg(bot, processo, img, mensagem):
for filename in os.listdir(base_path):
try:
file_path = base_path+filename
_arquivolock.acquire()
file = open(file_path,"r")
infos = json.loads(file.read())
file.close()
_arquivolock.release()
envia = None
if processo in infos["inscritos"]:
msg = ""
msg += "`"+processo+":` "+mensagem
bot.sendMessage(
chat_id=infos["cod"],
text=msg,
parse_mode=telegram.ParseMode.MARKDOWN)
bot.send_photo(chat_id=infos["cod"], photo=open(img, 'rb'))
except Exception as e:
a = open(base_path+"erros","a")
a.write(str(e)+"\n")
a.close()
raise
class Enviador():
def __init__(self):
file = open("~/apis/telegram","r")
key = file.read()
file.close()
self.bot = telegram.Bot(token=key)
def envia_notificacao(self, processo, mensagem):
t = threading.Thread(target=envia,args=[self.bot,processo,mensagem])
t.start()
return
def envia_img(self, processo, img, msg):
t = threading.Thread(target=enviaImg,args=[self.bot,processo,img,msg])
t.start()
return
# if __name__ == '__main__':
# e = Enviador()
# for i in range(10):
# e.envia_notificacao("devteste",str(i)+"%")
# print "Fim"