Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
import random
from urllib.parse import parse_qs
import webbrowser
import pickle
from datetime import datetime, timedelta
import vk
APP_ID = <Номер приложения>
# file, where auth data is saved
AUTH_FILE = '.auth_data'
# chars to exclude from filename
FORBIDDEN_CHARS = '/\\\?%*:|"<>!'
def get_saved_auth_params():
access_token = None
user_id = None
with open(AUTH_FILE, 'rb') as pkl_file:
token = pickle.load(pkl_file)
expires = pickle.load(pkl_file)
uid = pickle.load(pkl_file)
if < expires:
access_token = token
user_id = uid
except IOError:
return access_token, user_id
def save_auth_params(access_token, expires_in, user_id):
expires = + timedelta(seconds=int(expires_in))
with open(AUTH_FILE, 'wb') as output:
pickle.dump(access_token, output)
pickle.dump(expires, output)
pickle.dump(user_id, output)
def get_auth_params():
auth_url = ("{app_id}"
redirected_url = input("Paste here url you were redirected:\n")
aup = parse_qs(redirected_url)
aup['access_token'] = aup.pop(
save_auth_params(aup['access_token'][0], aup['expires_in'][0],
return aup['access_token'][0], aup['user_id'][0]
def get_api(access_token):
session = vk.Session(access_token=access_token)
return vk.API(session)
def send_message(api, user_id, message, **kwargs):
data_dict = {
'user_id': user_id,
'message': message,
return api.messages.send(**data_dict)
def get_texts():
with open('text.txt', 'r') as fio:
def main():
access_token, _ = get_saved_auth_params()
if not access_token or not _:
access_token, _ = get_auth_params()
api = get_api(access_token)
texts = get_texts()
for friend in api.friends.get():
res = send_message(api, user_id=friend, message=random.choice(texts))
if __name__ == '__main__':
Поздравляю с НГ
Новый год, Новый год, желаю счастья!
Хватит жрать за столом! Весь год будешь жрать
Удачи в Новом Году
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment