forked from seguimos/chat-broadcast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (62 loc) · 2 KB
/
main.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
import os
import subprocess
from dotenv import load_dotenv
load_dotenv()
wid_input = os.getenv('WID_INPUT')
executable = os.getenv('EXECUTABLE')
logFilename = "output.txt"
searchFilename = "searchgroups.toml"
baseFilename = "basefile.toml"
tomlFilename = "matterbridge.toml"
search_cmd = f'./{executable} -conf {searchFilename} > {logFilename}'
matterbrige_cmd = f'./{executable} -conf {tomlFilename}'
# Genera un bridge-in con el wid entregado
def generate_in(wid):
return f'''
# 0
[[gateway.in]]
account="whatsapp.mywhatsapp"
channel="{wid}"
'''
# Genera un bridge-out con el wid entregado
def generate_out(wid, i):
return f'''
# {i}
[[gateway.out]]
account="whatsapp.mywhatsapp"
channel="{wid}"
'''
# Genera un archivo toml con los wids entregados
def generate_toml(wid_input, wids_output, basefile=baseFilename, outfile=tomlFilename):
content = ""
with open(basefile, 'r') as file:
content += file.read()
content += generate_in(wid_input)
for i, wid in enumerate(wids_output):
content += generate_out(wid, i+1)
with open(outfile, 'w') as file:
file.write(content)
return content
if __name__ == "__main__":
wids_output = set()
while True:
print("Actualizando grupos...")
os.system(search_cmd)
with open(logFilename, 'r') as file:
actual_wids = set()
lines = file.readlines()
for line in lines:
if "@g.us" in line:
# Se accede al whatsapp id (wid)
wid = line.split(' ')[2].split('"')[1]
# Se añade el wid al set
actual_wids.add(wid)
actual_wids.discard(wid_input)
generate_toml(wid_input, actual_wids)
# Ejecuta la configuración por 1 hora
print("Ejecutando la configuración por 1 hora...")
p = subprocess.Popen(matterbrige_cmd.split())
try:
p.wait(60*60)
except subprocess.TimeoutExpired:
p.kill()