-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathviber_contacts.py
95 lines (74 loc) · 2.53 KB
/
viber_contacts.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
import requests
import argparse
import json
CONFIG = {}
def set_webhook():
payload = {
"auth_token": CONFIG['token'],
"url":"https://proton.me/",
"event_types":[
"failed"
],
"send_name": False,
"send_photo": False
}
r = requests.post("https://chatapi.viber.com/pa/set_webhook", json=payload)
print(r.text)
def get_account_info():
r = requests.post("https://chatapi.viber.com/pa/get_account_info", json={"auth_token": CONFIG['token']})
if r.status_code == 200:
print(r.json())
return r.json()["members"][0]["id"]
else:
return None
def send_contact_message(phone_number: str):
if CONFIG['uid'] is None or CONFIG['uid'] == '':
print("Error in uid")
raise Exception("Something went wrong uid is None or empty")
payload = {
"auth_token": CONFIG['token'],
"from": CONFIG['uid'],
"type": "contact",
"contact": {
"name":"a",
"phone_number":phone_number
}
}
r = requests.post("https://chatapi.viber.com/pa/post", json=payload)
print(r.text)
if r.status_code == 200:
print("Sent message for - {}".format(phone_number))
def send_bulk_message(fname):
phones = [x.strip() for x in open(fname, 'r').read().split('\n')]
for phone in phones:
send_contact_message(phone)
def load_config():
global CONFIG
CONFIG = json.load(open('config.json','rb'))
if CONFIG['token'] == '':
raise Exception("Please specify your TOKEN")
if CONFIG['first_time'] == 1:
set_webhook()
CONFIG['first_time'] = 0
json.dump(CONFIG, open('config.json', 'w'))
if CONFIG['uid'] == '':
uid = get_account_info()
print(uid)
if uid is None:
raise Exception("Something went wrong uid is returned as None")
CONFIG['uid'] = uid
json.dump(CONFIG, open('config.json', 'w'))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p','--phone', required=False, type=str, help="phone number +7...")
parser.add_argument('-l','--list', required=False, type=str, help="list with phone numbers")
args = parser.parse_args()
load_config()
if not (args.phone and args.list) and (args.phone or args.list):
if args.phone:
send_contact_message(args.phone)
else:
send_bulk_message(args.list)
else:
parser.print_help()
print("########## All messages sent ###########")