def main(): vk_session = vk_api.VkApi(login='******', password='******', app_id='2685278') vk_session.auth(token_only=True) longpoll = VkLongPoll(vk_session) vks = vk_session print("BOT STARTED!") def send(peer_id, message): m = vks.method("messages.send", { "peer_id": peer_id, "random_id": 0, "message": message }) return m for event in longpoll.listen(): try: if event.type == VkEventType.MESSAGE_NEW and event.text.lower( ) == "!help": send( event.peer_id, "1. !photo {кол-во} {альбом}\n2. !music {кол-во} - загрузка музыки\n3. !video {кол-во} {альбом} {ссылка} - загрузка видео" ) if event.type == VkEventType.MESSAGE_NEW and event.text.lower( )[:6] == "!music": count = event.text.lower()[6:].split()[0] m = random.randint(0, 100000000) upload = VkUpload(vk_session) for x in range(int(count)): s = upload.audio(audio="face.mp3", artist=f"test {m}", title=f"test2 {m}") m += 1 send(event.peer_id, f"Загрузку музыки закончил!\nколичество: {count}") if event.type == VkEventType.MESSAGE_NEW and event.text.lower( )[:6] == "!video": count = event.text.lower()[6:].split()[0] al = event.text[6:].split()[1] htps = event.text[6:].split()[2] upload = VkUpload(vk_session) for x in range(int(count)): s = upload.video(link=htps, name="видео", description="видео", album_id=al) send(event.peer_id, f"Загрузку видео закончил!\nколичество: {count}") if event.type == VkEventType.MESSAGE_NEW and event.text.lower( )[:6] == "!photo": count = event.text[6:].split()[0] al = event.text[6:].split()[1] upload = VkUpload(vk_session) for x in range(int(count)): s = upload.photo(photos="lol.png", album_id=int(al)) vks.method( "messages.send", { "peer_id": event.peer_id, "random_id": 0, "message": f"Загрузку окончил!\ncount: {count}" }) except Exception as s: print(f"Error: {s}")
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()