Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- from flask import Flask, request, json
- from time import sleep
- from threading import Thread
- from pydub import AudioSegment
- import threading
- import vk_api
- import random
- import re
- import time
- import shutil
- import sqlite3
- import requests
- import datetime
- import hashlib
- import os
- import struct
- import wave
- vk = vk_api.VkApi(token="f3bed5bc7d5f7be64512d0e36cdf8e1048475074e068902f72b58f94c8ad8ca23325201b2d005c6fc7607")
- vkUser =vk_api.VkApi(token="")
- app = Flask(__name__)
- conn = sqlite3.connect("vkDB.db", check_same_thread=False) # ��� :memory: ����� ��������� � RAM
- cursor = conn.cursor()
- reg = []
- def user_id(name):
- time_now = datetime.datetime.now()
- text = hex(int(f"{time_now.hour}{time_now.minute}{time_now.second}{time_now.year}{time_now.month}{time_now.day}"))
- return f"{name}_{text}
- def sendAttach(attach):
- vk.method("messages.send",
- {
- "peer_id": id,
- "random_id": random.randint(1, 2147483647),
- "attachment": attach
- })
- @app.route('/', methods = ["POST"])
- def main():
- data = json.loads(request.data)
- if data["type"] == "confirmation":
- return "1b095f74"
- if data["type"] == "message_new":
- object = data["object"]
- id = object["peer_id"]
- msgID = object["id"]
- body = object["text"]
- fromID = object["from_id"]
- attachments = object["attachments"]
- def sendMessage(text):
- vk.method("messages.send",
- {
- "peer_id": id,
- "random_id": random.randint(1, 2147483647),
- "message": text
- })
- try:
- sql = "SELECT * FROM users WHERE user_id=" + str(id)
- cursor.execute(sql)
- r = cursor.fetchall()
- if body == "?":
- sendMessage(len(r))
- if len(r) == 0:
- try:
- sql = "INSERT INTO users VALUES (NULL,'"+ str(id) +"',NULL, 0,0,0)"
- cursor.execute(sql)
- conn.commit()
- sendMessage("Вы успешно зарегистрировались! \n")
- except Exception as e:
- sendMessage(str(e))
- if body == "/test":
- if len(object['reply_message']) != 0 :
- if object['reply_message']['attachments'][0]['type'] == "audio_message":
- link = object['reply_message']['attachments'][0]['audio_message']['link_mp3']
- response = requests.get(link, stream=True)
- with open( str(id)+'.mp3', 'wb') as out_file:
- shutil.copyfileobj(response.raw, out_file)
- del response
- sound = AudioSegment.from_mp3(""+str(id)+".mp3")
- sound.export(""+str(id)+".wav", format="wav")
- url = vk.method('docs.getMessagesUploadServer', {'type': 'audio_message', "peer_id":id})['upload_url']
- files = [('file', (""+str(id)+".wav", open(""+str(id)+".wav", 'rb')))]
- url2 = requests.post(url, files=files).text
- response = json.loads(url2)['file']
- response2 = vk.method("docs.save",
- {
- "file":response
- })
- doc_id = response2['audio_message']['id']
- owner_id = response2['audio_message']['owner_id']
- attach = "doc"+str(owner_id)+"_"+str(doc_id)
- sendMessage(str(attach))
- vk.method("messages.send",
- {
- "peer_id": id,
- "random_id": random.randint(1, 2147483647),
- "attachment": attach
- })
- os.remove(""+str(id)+".wav")
- except Exception as e:
- vk.method("messages.send",
- {
- "peer_id": 379586207,
- "random_id": random.randint(1, 2147483647),
- "message": "ошибка, проверьте логи; " + repr(e)
- })
- return "ok"
- #if __name__ == "__main__":
- # app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement