-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend2chat.py
More file actions
65 lines (55 loc) · 1.93 KB
/
send2chat.py
File metadata and controls
65 lines (55 loc) · 1.93 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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# dependencies: []
import time
import traceback
import requests
import vk_api
from vk_api.utils import get_random_id
import sys
import os
import toml
path = f'{os.path.abspath(os.curdir)}/'
try:
config = toml.load('./configuration.toml') # если импортируется из корня
except FileNotFoundError:
try: # на случай, если вызывается из каки-то подпапок типа ./server или ./update ... может сломаться...
config = toml.load('../configuration.toml') # если импортируется из папки server
except FileNotFoundError:
sys.exit()
def send_message(message, vk, chat_id):
try:
vk.messages.send(
chat_id=chat_id,
random_id=get_random_id(),
message=message)
except vk_api.exceptions.ApiError as vk_error:
if "[917]" in str(vk_error):
print("Деда нет в данной конфе")
else:
raise Exception(vk_error)
except requests.exceptions.ConnectionError:
time.sleep(5)
send_message(message, vk, chat_id)
except Exception as e:
send_message(f'Кто-то пытался пырнуть деда, но произошла ошибка. Сообщение: {message}, chat_id={chat_id}.\n'
f'Ошибка: {traceback.format_exc()}', vk, chat_id)
token = config.get('Kiberded').get('token')
vk_session = vk_api.VkApi(token=token)
vk = vk_session.get_api()
message = ''
k = 0
chat_id = 1
for param in sys.argv:
if k == 0:
k += 1
else:
if k == 1 and "--chat_id=" in str(param):
chat_id_str = str(param)[10:]
try:
chat_id = int(chat_id_str)
except ValueError:
chat_id = 1
else:
message += str(param) + ' '
send_message(message, vk, chat_id)