Callback API is a tool for tracking user activity in your VKontakte community.
import vk_api session = vk_api.VkApi(token='{ACCESS_TOKEN}') print(session.method('users.get', {'user_ids': 210700286, 'fields': 'photo_50, city, verified'}))
import vk_api
Here we import the library we need. session = vk_api.VkApi(token='{ACCESS_TOKEN}')
With this line, we log in via access token (the ways to get it are available described in the documentation and on the Internet, I will not focus on them). Note: not all methods need authorization. About when it is required will be written in the documentation of the method on the site VKontakte. print(session.method('users.get', {'user_ids': 210700286, 'fields': 'photo_50, city'}))
This part of the code deserves special attention. What is going on here? We call the method method, passing it two arguments: the first is the name of the API method, the second is the dictionary of the parameters of this method. A list of all methods and their parameters is in the API documentation. In this case, we call the "users.get" method and pass the following parameters: "user_ids" - a list of user id for which we want to receive data, "fields": additional information about users: avatar and city of residence. The output of the program will be the following output line: [{'id': 210700286, 'first_name': 'Lindsey', 'last_name': 'Stirling', 'city': {'id': 5331, 'title': 'Los Angeles'}, 'photo_50': 'https://pp.userapi.com/c636821/v636821286/38a75/Ay-bEZoJZw8.jpg'}]
vk_api.exceptions.ApiError: [5] User authorization failed: no access_token passed.
.https://api.vk.com/method/{METHOD_NAME}?{PARAMS}&v={API_VERSION}
, where {METHOD_NAME} is the name of the method, {PARAMS} are the parameters of the called method, and {API_VERSION} - the API version that the server should use when generating the response. import requests data = requests.get('https://api.vk.com/method/{METHOD_NAME}'.format(METHOD_NAME='users.get'), params={'user_ids': 210700286, 'fields': 'photo_50, city'}).json() print(data)
import requests
Import library requests data = requests.get('https://api.vk.com/method/{METHOD_NAME}'.format(METHOD_NAME='users.get'), params={'user_ids': 210700286, 'fields': 'photo_50, city'}).json()
This line writes the server's response to the data variable. We give the function get two arguments: the address to which you want to make a request (in our case, a formatted string), and a dictionary of the parameters of this method. If the method is not called without access token, you need to add it to the dictionary with the key 'access_token'. The parameter “v” (API version) is not mandatory, since the latest version will be used by default, but if you need to use a different version, you must also add it to the dictionary with the 'v' key. The json () method that processes JSON objects is applied to the response sent from the server.{'response': [{'uid': 210700286, 'first_name': 'Lindsey', 'last_name': 'Stirling', 'city': 5331, 'photo_50': 'https://pp.userapi.com/c636821/v636821286/38a75/Ay-bEZoJZw8.jpg'}]}
.{'error': {'error_code': 5, 'error_msg': 'User authorization failed: no access_token passed.', 'request_params': [{'key': 'oauth', 'value': '1'}, {'key': 'method', 'value': 'user.get'}, {'key': 'user_ids', 'value': '210700286'}, {'key': 'fields', 'value': 'photo_50, city'}]}}
.Long Polling is a technology that allows you to receive information about new events using the "long requests". The server receives the request, but sends a response to it not immediately, but only when a certain event occurs (for example, a new incoming message arrives), or the specified timeout expires.In other words, when receiving a request from you, the server waits for an event to occur, about which it should notify you, and when it occurs, Long Poll server sends a response to your request containing information about the incident event.
key - the secret key of the session;
server - server address;
ts - the number of the last event from which you want to receive data.
import requests token = '' # access_token data = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # print(data)
{'key': '###############################', 'server': 'imv4.vk.com/im####', 'ts': 0000000000}
https://{$server}?act=a_check&key={$key}&ts={$ts}&wait=25&mode=2&version=2
, where {$ server}, {$ key} and {$ ts} are values from the dictionary with the keys 'server', 'key' and 'ts', respectively, wait is the time that the Long Poll server will wait for updates, and mode is the additional response options. Let's write a program that will make one request to the Long Poll server import requests token = '' # access_token params = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=90&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json() # Long Poll 90 2 print(response)
{'ts': 0000000000, 'updates': [[9, -999999999, 0, 1501588841]]}
. What does the answer mean and how can we continue working with it? import requests token = '' # access_token data = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # while True: response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=20&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json() # Long Poll 20 2 updates = response['updates'] if updates: # , for element in updates: # print(element) data['ts'] = response['ts'] #
[8, -999999999, 1, 1501592696]
[8, -999999999, 7, 1501592862]
[9, -999999999, 0, 1501592882]
[9, -999999999, 1, 1501592583]
[8, -999999999, 4, 1501592893]
[9, -999999999, 0, 1501592900]
[80, 0, 0]
[80, 1, 0]
# , while True: response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=20&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json() # Long Poll 20 2 updates = response['updates'] if updates: # , for element in updates: # action_code = element[0] # if action_code == 80: # print(' ', element[1]) # data['ts'] = response['ts'] #
elif action_code == 9:
[9, -000000000, 1, 1501744865]
user_id = element[1] * -1 # id , user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # , id = user_id timeout = bool(element[2]) # - last_visit = element[3] # Unix time
# "import time", if user['sex'] == 1: verb = ['', ''] else: verb = ['', ''] if timeout: print(user['first_name'], user['last_name'], verb[0], ' -. :', time.ctime(last_visit).split()[3]) else: print(user['first_name'], user['last_name'], verb[1], ' . :', time.ctime(last_visit).split()[3])
time.ctime(last_visit).split()[3]
'Thu Jan 1 03:00:00 1970'
'Thu Jan 1 03:00:00 1970'
. Since we only need the time of the last action on the site, we split this line using the split method and space the array, where index 0 stores the day of the week, index 1 - month, 2 - number, 3 - time, and 4 - year. We retrieve the element with the index 3, that is, time.[8, -6892937, 4, 1501750273]
. The second element in the array is the negative id of the user who has become online, the third is the platform from which the user logged in, and the fourth is the time of the last user action on the site at Unix time. We realize the received data in the code: user_id = element[1] * -1 # id , user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # , id = user_id platform = element[2] # last_visit = element[3] # Unix time if user['sex'] == 1: verb = '' else: verb = ''
# if platform == 1: platform = ' web- VK' elif platform == 2: platform = ' VK iPhone' elif platform == 3: platform = ' VK iPad' elif platform == 4: platform = ' VK Android' elif platform == 5: platform = ' VK Windows Phone' elif platform == 6: platform = ' VK Windows' elif platform == 7: platform = ' web- VK'
print(user['first_name'], user['last_name'], verb, ' ', platform, '', time.ctime(last_visit).split()[3])
[62, 000000000, 000]
. The second element of the array is the id of the user who is typing the messages, and the third is the ID of the conversation. Updates with code 61 are as follows: [61, 000000000, 1]
. Here the value has only the second element - the id of the user typing the message. Write the event handler: elif action_code == 61: user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': element[1]}).json()['response'][0] # print(user['first_name'], user['last_name'], ' ') elif action_code == 62: user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': element[1]}).json()['response'][0] # , chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': element[2], 'access_token': token}).json()['response']['title'] # print(user['first_name'], user['last_name'], ' "{}"'.format(chat))
[4, $ts, $flag, $id, $unixtime, $text, {'title': ' ... '}]
. Here $ ts is the number of the incoming event, $ flag is the message flags, $ id is the interlocutor's id or 2000000000 + conversation id (in the case of collective dialogs), $ unixtime is the time the message was added in Unix time, and the last element is the dictionary containing information about attachments, sender and changes in the settings of the conversation. For a start, let's look at where the message was added: in personal correspondence or conversation. If the message was sent in a conversation, then, as I have already written, in the $ id field there will be indicated a number resulting from the addition of 2,000,000,000 and chat_id (conversation identifier). If the message was added in personal correspondence, in the $ id field there will be the interlocutor's id, which is always less than 2,000,000,000 + chat_id of any conversation. , , $id — 2000000000 > 0, , , . , id , , 'from'. : elif action_code == 4: if element[3] - 2000000000 > 0: # , user_id = element[6]['from'] # id chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # if text: # , print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'], ' "{}"'.format(chat) + ':', text) else: user_id = element[3] # id user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # if text: # , print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'] + ':', text)
+1:
+2:
+4:
+8:
+16:
+32: .
+64: «»
+128: ( )
+256:
+512:
+65536: . ( ). <2.
summands = [] # , flag = element[2] # for number in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 65536]: # if flag & number: # , summands.append(number) # ,
if 2 not in summands:
elif action_code == 4: summands = [] # , flag = element[2] # for number in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 65536]: # if flag & number: # , summands.append(number) # , if 2 not in summands: if element[3] - 2000000000 > 0: # , user_id = element[6]['from'] # id chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # if text: # , print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'], ' "{}"'.format(chat) + ':', text) else: user_id = element[3] # id user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # if text: # , print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'] + ':', text)
[[4, $ts, $flag, $id, $unixtime, $text, {'attach1_type': 'photo', 'attach1': '$photoId', 'attach2_type': 'video', 'attach2': '$videoId', 'attach3_type': 'audio', 'attach3': '$audioId', 'attach4_type': 'doc', 'attach4': '$docId', 'geo': '2_SVK-le', 'geo_provider': '4', 'title': ' ... '}]
. , API . (photos.getById docs.getById) ( , ). - , . , ( ) . if 512 in summands: # , - index = 1 photos = [] # id docs = [] # id media_type = 'attach1_type' while media_type in element[6].keys(): # , - media_type = element[6]['attach{}_type'.format(index)] # , if media_type == 'photo': # photos.append(element[6]['attach{}'.format(index)]) # id elif media_type == 'doc': # docs.append(element[6]['attach{}'.format(index)]) # id index += 1 # media_type = 'attach{}_type'.format(index) change = lambda ids, type_: requests.get('https://api.vk.com/method/{}.getById'.format(type_), params={type_: ids, 'access_token': token}).json() # , if photos: # , photos = change(', '.join(photos), 'photos') # , photos if 'response' in photos.keys(): photos = [attachment['src_xbig'] for attachment in photos['response']] # print(' :', ', '.join(photos)) else: pass # , if docs: # , docs = change(', '.join(docs), 'docs') # , docs if 'response' in docs.keys(): docs = [attachment['url'] for attachment in docs['response']] # print(' :', ', '.join(docs)) else: pass # ,
if text:
, , - .[4, $ts, $flag, $chat_id, $unixtime, '', {'source_act': 'chat_title_update', 'source_text': ' ', 'source_old_text': ' ', 'from': '$id'}]
[4, $ts, $flag, $chat_id, $unixtime, '', {'attach1_type': 'photo', 'attach1': '247178624_456242629', 'source_act': 'chat_photo_update', 'from': '247178624'}]
[4, $ts, $flag, $chat_id, $unixtime, '', {'source_act': 'chat_invite_user', 'source_mid': '$added_user_id', 'from': '$adder_id'}]
[4, $ts, $flag, $chat_id, $unixtime, '', {'source_act': 'chat_kick_user', 'source_mid': '&removed_user_id', 'from': '&remover_id'}]
[4, $ts, $flag, $chat_id, $unixtime, '', {'source_act': 'chat_create', 'source_text': '', 'from': '$creator_id'}]
elif action_code == 4: if 'source_act' not in element[6].keys(): # <, > else: source_act = element[6] if source_act['source_act'] == 'chat_title_update': # changer_id = source_act['from'] # id , source_text = source_act['source_text'] # source_old_text = source_act['source_old_text'] # changer = requests.get('https://api.vk.com/method/users.get', params={'user_ids': changer_id, 'fields': 'sex'}).json()['response'][0] # , if changer['sex']: verb = '' else: verb = '' print(changer['first_name'], changer['last_name'], verb, ' "{}" "{}"'.format(source_old_text, source_text)) elif source_act['source_act'] == 'chat_photo_update': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # user_id = source_act['from'] # id , photo_id = source_act['attach1'] # id photo = requests.get('https://api.vk.com/method/photos.getById', params={'photos': photo_id, 'access_token': token}).json() # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # , if 'error' not in photo.keys(): # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}" '.format(chat), photo['response'][0]['src_xbig']) else: pass # , elif source_act['source_act'] == 'chat_invite_user': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # invited_id = source_act['source_mid'] # id inviter_id = source_act['from'] # id if invited_id == inviter_id: # - user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': inviter_id, 'fields': 'sex'}).json()['response'][0] # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}"'.format(chat)) else: inviter_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': inviter_id, 'fields': 'sex'}).json()['response'][0] # invited_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': invited_id, 'name_case': 'acc'}).json()['response'][0] # if inviter_user['sex']: verb = '' else: verb = '' print(inviter_user['first_name'], inviter_user['last_name'], verb, ' "{}"'.format(chat), invited_user['first_name'], invited_user['last_name']) elif source_act['source_act'] == 'chat_kick_user': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # removed_id = source_act['source_mid'] # id remover_id = source_act['from'] # id if removed_id == remover_id: # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': remover_id, 'fields': 'sex'}).json()['response'][0] # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}"'.format(chat)) else: remover_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': remover_id, 'fields': 'sex'}).json()['response'][0] # removed_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': removed_id, 'name_case': 'acc'}).json()['response'][0] # if remover_user['sex']: verb = '' else: verb = '' print(remover_user['first_name'], remover_user['last_name'], verb, ' "{}"'.format(chat), removed_user['first_name'], removed_user['last_name']) elif source_act['source_act'] == 'chat_create': chat = source_act['source_text'] # creator_id = source_act['from'] # id creator = requests.get('https://api.vk.com/method/users.get', params={'user_ids': creator_id, 'fields': 'sex'}).json()['response'][0] # , if creator['sex']: verb = '' else: verb = '' print(creator['first_name'], creator['last_name'], verb, ' "{}"'.format(chat))
response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=20&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json()['response'] # Long Poll 20 2
. , «error 2», , &key . : response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=20&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json() # Long Poll 20 2 try: updates = response['updates'] except KeyError: # KeyError, key , data = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # continue # ,
&_amp
( , ). . , sub re ( !). import re # <...> symbols = {'<br>': '\n', '&_amp;': '&', '&_quot;': '"', '&_lt;': '<', '&_gt;': '>', '&_tilde;': '~', '&_circ;': 'ˆ', '&_ndash;': '–', '&_mdash;': '—', '&_euro;': '€', '&_permil;': '‰'} # for code, value in symbols.items(): text = re.sub(code, value, text)
import re import time import requests token = '' # access_token data = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # while True: response = requests.get('https://{server}?act=a_check&key={key}&ts={ts}&wait=20&mode=2&version=2'.format(server=data['server'], key=data['key'], ts=data['ts'])).json() # Long Poll 20 2 try: updates = response['updates'] except KeyError: # KeyError, key , data = requests.get('https://api.vk.com/method/messages.getLongPollServer', params={'access_token': token}).json()['response'] # continue # , if updates: # , for element in updates: # action_code = element[0] if action_code == 80: # print(' ', element[1]) # elif action_code == 9: user_id = element[1] * -1 # id , user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # id = user_id timeout = bool(element[2]) # - last_visit = element[3] # if user['sex'] == 1: verb = ['', ''] else: verb = ['', ''] if timeout: print(user['first_name'], user['last_name'], verb[0], ' -. :', time.ctime(last_visit).split()[3]) else: print(user['first_name'], user['last_name'], verb[1], ' . :', time.ctime(last_visit).split()[3]) elif action_code == 8: user_id = element[1] * -1 # id , user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # id = user_id platform = element[2] # last_visit = element[3] # Unix time if user['sex'] == 1: verb = '' else: verb = '' # if platform == 1: platform = ' web- VK' elif platform == 2: platform = ' VK iPhone' elif platform == 3: platform = ' VK iPad' elif platform == 4: platform = ' VK Android' elif platform == 5: platform = ' VK Windows Phone' elif platform == 6: platform = ' VK Windows' elif platform == 7: platform = ' web- VK' print(user['first_name'], user['last_name'], verb, ' ', platform, '', time.ctime(last_visit).split()[3]) elif action_code == 61: user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': element[1]}).json()['response'][0] # print(user['first_name'], user['last_name'], ' ') elif action_code == 62: user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': element[1]}).json()['response'][0] # , chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': element[2], 'access_token': token}).json()['response']['title'] # print(user['first_name'], user['last_name'], ' "{}"'.format(chat)) elif action_code == 4: if 'source_act' not in element[6].keys(): summands = [] # , flag = element[2] # for number in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 65536]: # if flag & number: # , summands.append(number) # , if 2 not in summands: if element[3] - 2000000000 > 0: # , user_id = element[6]['from'] # id chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # symbols = {'<br>': '\n', '&_amp;': '&', '&_quot;': '"', '&_lt;': '<', '&_gt;': '>', '&_tilde;': '~', '&_circ;': 'ˆ', '&_ndash;': '–', '&_mdash;': '—', '&_euro;': '€', '&_permil;': '‰'} # for code, value in symbols.items(): text = re.sub(code, value, text) print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'], ' "{}"'.format(chat) + ':', text) else: user_id = element[3] # id user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'name_case': 'gen'}).json()['response'][0] # , time_ = element[4] # text = element[5] # symbols = {'<br>': '\n', '&_amp;': '&', '&_quot;': '"', '&_lt;': '<', '&_gt;': '>', '&_tilde;': '~', '&_circ;': 'ˆ', '&_ndash;': '–', '&_mdash;': '—', '&_euro;': '€', '&_permil;': '‰'} # for code, value in symbols.items(): text = re.sub(code, value, text) print(time.ctime(time_).split()[3] + ':', ' ', user['first_name'], user['last_name'] + ':', text) if 512 in summands: # , - index = 1 photos = [] # id docs = [] # id media_type = 'attach1_type' while media_type in element[6].keys(): # , - media_type = element[6]['attach{}_type'.format(index)] # , if media_type == 'photo': # photos.append(element[6]['attach{}'.format(index)]) # id elif media_type == 'doc': # docs.append(element[6]['attach{}'.format(index)]) # id index += 1 # media_type = 'attach{}_type'.format(index) change = lambda ids, type_: requests.get('https://api.vk.com/method/{}.getById'.format(type_), params={type_: ids, 'access_token': token}).json() # , if photos: # , photos = change(', '.join(photos), 'photos') # , photos if 'response' in photos.keys(): photos = [attachment['src_xbig'] for attachment in photos['response']] # print(' :', ', '.join(photos)) else: pass # , if docs: # , docs = change(', '.join(docs), 'docs') # , docs if 'response' in docs.keys(): docs = [attachment['url'] for attachment in docs['response']] # print(' :', ', '.join(docs)) else: pass # , else: source_act = element[6] if source_act['source_act'] == 'chat_title_update': # changer_id = source_act['from'] # id , source_text = source_act['source_text'] # source_old_text = source_act['source_old_text'] # changer = requests.get('https://api.vk.com/method/users.get', params={'user_ids': changer_id, 'fields': 'sex'}).json()['response'][0] # , if changer['sex']: verb = '' else: verb = '' print(changer['first_name'], changer['last_name'], verb, ' "{}" "{}"'.format(source_old_text, source_text)) elif source_act['source_act'] == 'chat_photo_update': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # user_id = source_act['from'] # id , photo_id = source_act['attach1'] # id photo = requests.get('https://api.vk.com/method/photos.getById', params={'photos': photo_id, 'access_token': token}).json() # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': user_id, 'fields': 'sex'}).json()['response'][0] # , if 'error' not in photo.keys(): # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}" '.format(chat), photo['response'][0]['src_xbig']) else: pass # , elif source_act['source_act'] == 'chat_invite_user': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # invited_id = source_act['source_mid'] # id inviter_id = source_act['from'] # id if invited_id == inviter_id: # - user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': inviter_id, 'fields': 'sex'}).json()['response'][0] # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}"'.format(chat)) else: inviter_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': inviter_id, 'fields': 'sex'}).json()['response'][0] # invited_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': invited_id, 'name_case': 'acc'}).json()['response'][0] # if inviter_user['sex']: verb = '' else: verb = '' print(inviter_user['first_name'], inviter_user['last_name'], verb, ' "{}"'.format(chat), invited_user['first_name'], invited_user['last_name']) elif source_act['source_act'] == 'chat_kick_user': chat_id = element[3] - 2000000000 # id chat = requests.get('https://api.vk.com/method/messages.getChat', params={'chat_id': chat_id, 'access_token': token}).json()['response']['title'] # removed_id = source_act['source_mid'] # id remover_id = source_act['from'] # id if removed_id == remover_id: # user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': remover_id, 'fields': 'sex'}).json()['response'][0] # if user['sex']: verb = '' else: verb = '' print(user['first_name'], user['last_name'], verb, ' "{}"'.format(chat)) else: remover_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': remover_id, 'fields': 'sex'}).json()['response'][0] # removed_user = requests.get('https://api.vk.com/method/users.get', params={'user_ids': removed_id, 'name_case': 'acc'}).json()['response'][0] # if remover_user['sex']: verb = '' else: verb = '' print(remover_user['first_name'], remover_user['last_name'], verb, ' "{}"'.format(chat), removed_user['first_name'], removed_user['last_name']) elif source_act['source_act'] == 'chat_create': chat = source_act['source_text'] # creator_id = source_act['from'] # id creator = requests.get('https://api.vk.com/method/users.get', params={'user_ids': creator_id, 'fields': 'sex'}).json()['response'][0] # , if creator['sex']: verb = '' else: verb = '' print(creator['first_name'], creator['last_name'], verb, ' "{}"'.format(chat)) data['ts'] = response['ts'] #
Source: https://habr.com/ru/post/335106/
All Articles