Created
December 11, 2019 21:27
-
-
Save AppLoidx/7bd3a0620877f8600028aada09800360 to your computer and use it in GitHub Desktop.
Simple example for pass image from pixiv
This file contains hidden or 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
import random | |
import requests | |
import vk_api | |
from com.apploidxxx.config import VK_API_KEY as API_KEY | |
from vk_api.longpoll import VkLongPoll, VkEventType | |
# Auth as vk group with API key | |
vk = vk_api.VkApi(token=API_KEY) | |
# Longpoll for messages | |
longpoll = VkLongPoll(vk) | |
def write_msg(user_id: int, message: str) -> None: | |
""" | |
Send message for user | |
:param user_id: vk id | |
:param message: plain text | |
:return: void | |
""" | |
vk.method('messages.send', {'user_id': user_id, 'message': message, 'random_id': random.randint(1, 1000)}) | |
upload = vk_api.VkUpload(vk) | |
session = requests.Session() | |
photo = upload.photo_messages( | |
photos=session.get( | |
'https://i.pximg.net/img-original/img/2018/12/31/19/31/37/72405800_p0.jpg', | |
headers={'Referer': 'https://app-api.pixiv.net/'}, | |
stream=True).raw) | |
photo_url = 'photo{}_{}'.format(photo[0]['owner_id'], photo[0]['id']) | |
attachments = [photo_url] | |
# Main loop - Simple example for work with longpoll | |
for event in longpoll.listen(): | |
if event.type == VkEventType.MESSAGE_NEW: | |
if event.to_me: | |
request = event.text | |
if request == "привет": | |
attachment = ','.join(attachments) | |
vk.method('messages.send', { | |
'user_id': event.user_id, | |
'random_id': random.randint(1, 1000), | |
'attachment': attachment}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment