Пример #1
0
def cmd(api, message, args, uploader: VkUpload):
    attach = message.get('attachments')
    reply = message.get('reply_message')
    if len(attach) == 0 and reply is None:
        api.messages.send(
            peer_id=message['peer_id'],
            random_id=0,
            message=f"{config.prefixes['error']} Необходимо ответить на сообщение с фотографией / стикером, либо прикрепить фотографию.",
            reply_to=message['id']
        )
        return

    try:
        if reply is not None:
            _type = reply['attachments'][0]['type']
            if reply['attachments'][0].get('sticker') is not None:
                img = reply['attachments'][0]['sticker']['images'].pop()['url']
            elif reply['attachments'][0].get('photo') is not None:
                img = reply['attachments'][0]['photo']['sizes'].pop()['url']
            else:
                raise Exception()
        else:
            _type = "photo"
            img = attach[0]['photo']['sizes'].pop()['url']
    except:
        api.messages.send(
            peer_id=message['peer_id'],
            random_id=0,
            message=f"{config.prefixes['error']} Необходимо ответить на сообщение с фотографией / стикером, либо прикрепить фотографию.",
            reply_to=message['id']
        )
        return

    filename = "files/" + os.path.basename(img).split('?')[0]
    if len(filename.split('.')) < 2: filename += ".png"

    r = requests.get(img)
    with open(filename, 'wb') as f:
        f.write(r.content)

    os.system(f"convert {filename} -negate {filename}")

    if _type == "sticker":
        uploaded = uploader.graffiti(filename, peer_id=message['peer_id'])['graffiti']
        attach = f"graffiti{uploaded['owner_id']}_{uploaded['id']}"
    else:
        uploaded = uploader.photo_messages(filename, peer_id=message['peer_id'])[0]
        attach = f"photo{uploaded['owner_id']}_{uploaded['id']}"

    os.remove(filename)
    api.messages.send(
        peer_id=message['peer_id'],
        random_id=0,
        attachment=attach,
        reply_to=message['id']
    )
    return
Пример #2
0
def cmd(api, message, args, uploader: VkUpload):
    attach = message.get('attachments')
    reply = message.get('reply_message')
    if len(attach) == 0 and reply is None:
        api.messages.send(
            peer_id=message['peer_id'],
            random_id=0,
            message=f"{config.prefixes['error']} Необходимо ответить на сообщение с фотографией / стикером, либо прикрепить фотографию.",
            reply_to=message['id']
        )
        return

    try:
        if reply is not None:
            _type = reply['attachments'][0]['type']
            if reply['attachments'][0].get('sticker') is not None:
                img = reply['attachments'][0]['sticker']['images'].pop()['url']
            elif reply['attachments'][0].get('photo') is not None:
                img = reply['attachments'][0]['photo']['sizes'].pop()['url']
            else:
                raise Exception()
        else:
            _type = "photo"
            img = attach[0]['photo']['sizes'].pop()['url']
    except:
        api.messages.send(
            peer_id=message['peer_id'],
            random_id=0,
            message=f"{config.prefixes['error']} Необходимо ответить на сообщение с фотографией / стикером, либо прикрепить фотографию.",
            reply_to=message['id']
        )
        return

    filename = "files/" + os.path.basename(img).split('?')[0]
    if len(filename.split('.')) < 2: filename += ".png"

    r = requests.get(img)
    with open(filename, 'wb') as f:
        f.write(r.content)

    if len(args) >= 2:
        if args[1] == "1" or args[1] is None:
            size = '50x50%'
        elif args[1] == "2":
            size = '45x45%'
        elif args[1] == "3":
            size = '40x40%'
        elif args[1] == "4":
            size = '35x35%'
        elif args[1] == "5":
            size = '30x30%'
        elif args[1] == "6":
            size = '25x25%'
        elif args[1] == "7":
            size = '20x20%'
        elif args[1] == "8":
            size = '15x15%'
        elif args[1] == "9":
            size = '10x10%'
        elif args[1] == "10":
            size = '5x5%'
        else:
            api.messages.send(
                peer_id=message['peer_id'],
                random_id=0,
                message=f"{config.prefixes['invalid']} Правильное использование: /d [степень от 1 до 10]",
                reply_to=message['id']
            )
            return
    else:
        size = '50x50%'

    im = Image.open(filename)
    width, height = im.size
    os.system(f"convert {filename} -liquid-rescale {size}! -resize {width}x{height}\! {filename}")

    if _type == "sticker":
        uploaded = uploader.graffiti(filename, peer_id=message['peer_id'])['graffiti']
        attach = f"graffiti{uploaded['owner_id']}_{uploaded['id']}"
    else:
        uploaded = uploader.photo_messages(filename, peer_id=message['peer_id'])[0]
        attach = f"photo{uploaded['owner_id']}_{uploaded['id']}"

    os.remove(filename)
    api.messages.send(
        peer_id=message['peer_id'],
        random_id=0,
        attachment=attach,
        reply_to=message['id']
    )
    return
Пример #3
0
def send_message(uid,
                 chat_id,
                 msg=None,
                 photo=None,
                 documents=None,
                 audio=None,
                 voice=None,
                 video=None,
                 sticker=None):
    try:
        session = get_session(uid)
        api = get_api(uid)
    except IndexError:
        return 0

    # Generate message id like in vk api
    msg_id = randint(1, 9223372036854775700)
    try:
        vchat_id = execute(
            f"select vchat_id from chats where chat_id = {chat_id}")[0][0]
        # if chat is conference
        if vchat_id is None:
            vchat_id = execute(
                f"select peer_id from chats where chat_id = {chat_id}")[0][0]
    except IndexError:
        # If telegram group not binded to vk chat stream
        return

    attachments = []
    if photo or documents or audio or voice or video or sticker:
        if photo:
            attach = photo
        elif documents:
            attach = documents
        elif audio:
            attach = audio
        elif voice:
            attach = voice
        elif video:
            attach = video
        elif sticker:
            attach = sticker
        else:
            attach = None

        uploader = VkUpload(session)
        attach_file = attach.get_file().download()
        if sticker:
            new_name = attach_file.split('.')[0] + '.png'
            try:
                im = Image.open(attach_file).convert("RGBA")
                im.save(new_name, "png")
            except UnidentifiedImageError:
                # Animated sticker
                return
            attach_file = new_name
        attach_file = open(attach_file, 'rb')
        file_name = attach_file.name

        if photo:
            upload_response = uploader.photo_messages(photos=attach_file,
                                                      peer_id=vchat_id)
            for file in upload_response:
                attachments.append(f"photo{file['owner_id']}_{file['id']}")

        if audio:
            _msg = "VK doesn't allow to upload music."
            if msg is None:
                msg = _msg
            else:
                msg += "\n" + _msg

        if voice:
            upload_response = uploader.audio_message(audio=attach_file,
                                                     peer_id=vchat_id)
            audio = upload_response['audio_message']
            attachments.append(
                f"audio_message{audio['owner_id']}_{audio['id']}")

        if documents:
            upload_response = uploader.document_message(attach_file,
                                                        peer_id=vchat_id)
            doc = upload_response['doc']
            attachments.append(f"doc{doc['owner_id']}_{doc['id']}")

        if video:
            video = uploader.video(attach_file, is_private=True)
            attachments.append(f"video{video['owner_id']}_{video['video_id']}")

        if sticker:
            upload_response = uploader.graffiti(attach_file, peer_id=vchat_id)
            graffiti = upload_response['graffiti']
            attachments.append(f"doc{graffiti['owner_id']}_{graffiti['id']}")

        attach_file.close()
        remove(file_name)

        attachments = ','.join(attachments)

    if msg is not None:
        api.messages.send(random_id=msg_id,
                          message=msg,
                          peer_id=vchat_id,
                          attachment=attachments)
    else:
        api.messages.send(random_id=msg_id,
                          peer_id=vchat_id,
                          attachment=attachments)

    api.account.setOffline()