-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotter.py
More file actions
146 lines (114 loc) · 4.18 KB
/
Copy pathbotter.py
File metadata and controls
146 lines (114 loc) · 4.18 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from telegram.ext import Updater, InlineQueryHandler, CommandHandler
import json, os, datetime
base_path = "~/Documents/telegram_bot/"
def doSubscribe(bot,update):
chat_id = update.message.chat_id
msg = update["message"]["text"]
msg = msg.split(" ")
if len(msg)!=2:
bot.sendMessage(chat_id=chat_id, text="Parametros invalidos :-(")
return
else:
try:
chat_id = str(chat_id)
file_dest = base_path+"subs/"
if not os.path.isdir(file_dest):
os.mkdir(file_dest)
file_dest += chat_id
sub = os.path.isfile(file_dest)
if not sub:
ativodate = datetime.datetime.now()
ativodate = str(ativodate)
obj = {"cod":chat_id,"status":"ativo","cadastro":ativodate,"desativo":None,"inscritos":[msg[1]],"mensagens_enviadas":{}}
aarquivo = open(file_dest,"w")
aarquivo.write(json.dumps(obj))
aarquivo.close()
else:
aarquivo = open(file_dest,"r")
infos = json.loads(aarquivo.read())
aarquivo.close()
if msg[1] not in infos["inscritos"]:
infos["inscritos"].append(msg[1])
aarquivo = open(file_dest,"w")
aarquivo.write(json.dumps(infos))
aarquivo.close()
else:
bot.sendMessage(chat_id=chat_id, text="Ja inscrito!")
return
bot.sendMessage(chat_id=chat_id, text="Inscrissao realizada com sucesso")
except Exception as e:
bot.sendMessage(chat_id=chat_id, text=str(e))
return
def getSubs(bot,update):
chat_id = update.message.chat_id
chat_id = str(chat_id)
try:
file_dest = base_path+"subs/"
if not os.path.isdir(file_dest):
os.mkdir(file_dest)
file_dest += chat_id
sub = os.path.isfile(file_dest)
if not sub:
bot.sendMessage(chat_id=chat_id, text="Voce ainda nao se inscreveu em nada")
else:
aarquivo = open(file_dest,"r")
infos = json.loads(aarquivo.read())
aarquivo.close()
inscricoes = "Suas inscricoes:\n"
for m in infos["inscritos"]:
inscricoes += " @"+m+"\n"
bot.sendMessage(chat_id=chat_id, text=inscricoes)
return
except Exception as e:
bot.sendMessage(chat_id=chat_id, text=str(e))
return
def delSubs(bot,update):
chat_id = update.message.chat_id
chat_id = str(chat_id)
msg = update["message"]["text"]
msg = msg.split(" ")
if len(msg)!=2:
bot.sendMessage(chat_id=chat_id, text="Parametros invalidos :-(")
return
sair = msg[1]
try:
file_dest = base_path+"subs/"
if not os.path.isdir(file_dest):
os.mkdir(file_dest)
file_dest += chat_id
sub = os.path.isfile(file_dest)
if not sub:
bot.sendMessage(chat_id=chat_id, text="Voce ainda nao se inscreveu em nada")
return
else:
aarquivo = open(file_dest,"r")
infos = json.loads(aarquivo.read())
aarquivo.close()
try:
infos["inscritos"].remove(sair)
except:
pass
aarquivo = open(file_dest,"w")
aarquivo.write(json.dumps(infos))
aarquivo.close()
bot.sendMessage(chat_id=chat_id, text="Inscricao em @"+sair+" cancelada")
return
except Exception as e:
bot.sendMessage(chat_id=chat_id, text=str(e))
return
def main():
file = open("~/apis/telegram","r")
key = file.read()
file.close()
updater = Updater(key)
dp = updater.dispatcher
dp.add_handler(CommandHandler('sub',doSubscribe))
dp.add_handler(CommandHandler('subscribe',doSubscribe))
dp.add_handler(CommandHandler('getsubs',getSubs))
dp.add_handler(CommandHandler('get',getSubs))
dp.add_handler(CommandHandler('del',delSubs))
dp.add_handler(CommandHandler('delSubs',delSubs))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()