forked from lifefilm/vkcommunity_bot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvkGroupApi.py
83 lines (52 loc) · 2.11 KB
/
vkGroupApi.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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import requests
import json
import time
import random
from colorama import *
init(autoreset=True)
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
appName='life'
#######################################################
def loadConfig(appName=appName):
import config
# print 'loaded config for:'+appName
access_token = config.vk[appName]['access_token']
peer_id = config.vk[appName]['peer_id']
return access_token, peer_id
def requestVk(metod,param='',appName=appName):
access_token, peer_id = loadConfig(appName)
vk_url="https://api.vk.com/method/"
vk_ver='5.53'
resp = requests.get(vk_url+metod,param+'&access_token={}&v={}'.format(access_token,vk_ver))
result = resp.json()
# print (json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False))
try:
if 'response' in result:
if 'items' in result['response']:
return result['response']['items']
else:
return result['response']
elif 'error' in result:
print (Fore.RED + 'error_msg: '+result['error']['error_msg'])
return False
except Exception as e:
return result
#######################################################
def messagesGetDialogs(appName=appName):
return requestVk('messages.getDialogs','',appName)
def messagesGet(count=30,appName=appName):
return requestVk('messages.get','&count={}'.format(count),appName)
def messagesGetHistory(user_id,start_message_id='',count=30,appName=appName):
return requestVk('messages.getHistory',
'&user_id={}&start_message_id={}&peer_id={}&count={}'
.format(user_id, start_message_id, peer_id, count),appName)
def messagesSend(user_id,chat_id=1,message='',attachment='',sticker_id='',appName=appName):
access_token, peer_id = loadConfig(appName)
result = requestVk('messages.send',
'&user_id={}&peer_id={}&chat_id={}&message={}&attachment={}&sticker_id={}'
.format(user_id,peer_id, chat_id,message,attachment,sticker_id),appName)
return result