Exemplo n.º 1
0
def audio_message(update, context):
    from app import create_app
    app = create_app(config_class=Config)
    app.app_context().push()

    audio = update.message.voice.get_file()
    audio = audio.download_as_bytearray()
    rand = str(random.randint(100000,1000000))
    filename =  rand+"note.ogg"
    with open(os.path.join(Config.UPLOAD_FOLDER, filename), 'wb') as f:
        f.write(audio)
    subprocess.run(['ffmpeg', '-i', os.path.join(Config.UPLOAD_FOLDER, filename), os.path.join(Config.UPLOAD_FOLDER, filename.replace('ogg', 'wav'))])
    with sr.AudioFile(os.path.join(Config.UPLOAD_FOLDER, filename.replace('ogg', 'wav'))) as s:
        r = sr.Recognizer()
        txt = r.listen(s)
        text = r.recognize_google(txt, language = 'ru-RU')
        note = Note()
        note.text = text
        note.sound_file = filename
        note.creator = User.query.filter_by(tg_id=update.message.chat_id).first().id
        db.session.add(note)
        db.session.commit()
        bot.send_message(
            chat_id=update.message.chat_id,
            text=f"Заметка сохранена"
        )
Exemplo n.º 2
0
def index():
    title = 'Главная'

    # Я поставил
    orders_iam_creator = current_user.get_orders_iam_creator()

    # Мне поставили
    orders_iam_executor = current_user.get_orders_iam_executor()

    create_order_form = CreateOrder()
    for order_type in OrderTypes.query.all():
        create_order_form.type.choices.append(
            (str(order_type.id), order_type.title))

    create_order_form.executors.choices.append(('0', 'не выбрано'))
    for tag in Tag.query.all():
        create_order_form.executors.choices.append((str(tag.id), tag.name))

    create_note_form = CreateNote()

    if create_order_form.validate_on_submit():
        all_fields = request.form
        custom_fields = {}
        personal_executors = []
        for field in all_fields:
            if 'field' in field.split('-'):
                custom_fields[field.split('-')[0]] = all_fields[field]
            if 'executor' in field.split('-'):
                personal_executors.append(User.query.get(field.split('-')[-1]))

        order = Order()
        order.creator = current_user.id
        order.base_order = create_order_form.baseorder.data
        order.title = create_order_form.title.data
        order.description = create_order_form.description.data
        order.description_sound = create_order_form.file.data
        order.priority = create_order_form.priority.data
        order.type = create_order_form.type.data
        order.interval = create_order_form.interval.data
        order.deadline = create_order_form.deadline.data

        for field in all_fields:
            if 'file_id' in field:
                order.description_sound = f'{all_fields["file_id"]}sound.ogg'
                # print(os.path.join(Config.UPLOAD_FOLDER, f'{all_fields["file_id"]}sound.wav'))

        order.status = 1

        if create_order_form.executors.data != '0':
            tag = Tag.query.filter(
                Tag.id == create_order_form.executors.data).first()
            for user in tag.users:
                order.executors.append(user)
        if personal_executors:
            for user in personal_executors:
                order.executors.append(user)

        order.reactions = json.dumps(custom_fields)

        db.session.add(order)
        db.session.commit()
        return redirect(request.referrer)

    if create_note_form.validate_on_submit():
        note = Note()
        note.creator = current_user.id
        note.text = create_note_form.text.data

        all_fields = request.form
        for field in all_fields:
            if 'file_id' in field:
                note.sound_file = os.path.join(
                    Config.UPLOAD_FOLDER, f'{all_fields["file_id"]}sound.wav')

        db.session.add(note)
        db.session.commit()
        return redirect(request.referrer)

    return render_template('main/index.html',
                           title=title,
                           create_order_form=create_order_form,
                           create_note_form=create_note_form,
                           orders_iam_creator=orders_iam_creator,
                           orders_iam_executor=orders_iam_executor,
                           custom_inputs=CustomInputs.query.all(),
                           list=list,
                           reversed=reversed)