Created
December 29, 2018 10:22
-
-
Save fortunto2/f67790379ad97cb69981dbee31c5d035 to your computer and use it in GitHub Desktop.
Рассылка сообщений через группу пользователям
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import os | |
import vk_api | |
# from vk_api import VkUpload | |
# from vk_api.utils import get_random_id | |
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType | |
def get_random_id(): | |
# не доступно из либы, вытащил сам в виде кода временно | |
import time | |
return int(time.time() * 10000000) | |
def main(): | |
session = requests.Session() | |
vk_token = os.getenv('VK_TOKEN') | |
vk_group = os.getenv('VK_GROUP') | |
assert vk_token, 'No VK token in .env' | |
assert vk_token, 'No VK_group_id in .env' | |
vk_session = vk_api.VkApi(token=vk_token) | |
vk = vk_session.get_api() | |
longpoll = VkBotLongPoll(vk_session, vk_group) | |
# upload = VkUpload(vk_session) # Для загрузки изображений | |
text = "Привет, как дела?" | |
users = ['3333333', '44444444', '5555555'] | |
for user_id in users: | |
print(f'----------{user_id}-----------') | |
try: | |
result = vk.messages.send( | |
user_id=user_id, | |
# attachment=','.join(attachments), | |
random_id=get_random_id(), | |
message=text | |
) | |
print('message_id: ', result) | |
except Exception as e: | |
print(e) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment