import sounddevice as sd
import numpy as np
duration = 5
fs = 44100
print("Запись...")
recording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
sd.wait()
print("Запись завершена, воспроизведение...")
sd.play(recording, fs)
sd.wait()
from fastapi import FastAPI, Request
from tgbot.main import tgbot
app = FastAPI()
@app.post('/api/bot')
async def tgbot_webhook_route(request: Request):
update_dict = await request.json()
await tgbot.update_bot(update_dict)
return ''
import asyncio
from aiogram import Bot, Dispatcher, Router
from tgbot.handlers import router
class TGBot:
def __init__(self, router: Router) -> None:
token = config('TOKEN')
self.bot = Bot(token)
self.dp = Dispatcher()
self.dp.include_router(router)
if not config('DEBUG', default=False):
loop = asyncio.get_event_loop()
loop.run_until_complete(self.set_commands())
loop.run_until_complete(self.set_webhook())
async def update_bot(self, update: dict) -> None:
await self.dp.feed_raw_update(self.bot, update)
await self.bot.session.close()
async def set_webhook(self):
webhook_url = config('WEBHOOK_URL')
# WEBHOOK_URL = адрес сайта/api/bot
await self.bot.set_webhook(webhook_url)
await self.bot.session.close()
tgbot = TGBot(router)
{
"rewrites": [
{ "source": "/(.*)", "destination": "/api/index" }
]
}
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import ReplyKeyboardMarkup
from aiogram.contrib.fsm_storage.memory import MemoryStorage
bot = Bot('')
storage = MemoryStorage()
dp = Dispatcher(bot, storage=MemoryStorage())
state_time = 'setTime'
kb = ReplyKeyboardMarkup(row_width=10, one_time_keyboard=True)
btns = (types.KeyboardButton(str(btn)) for btn in range(1, 31))
kb.add(*btns)
@dp.message_handler(commands=['start'])
async def dp_choose_user(message: types.Message):
await message.answer('Введите день:', reply_markup=kb)
await dp.current_state().set_state(state_time) # Вход в состояние ввода даты
@dp.message_handler(lambda message: message.text.isdigit() and 1 <= int(message.text) <= 30, state=state_time)
async def dp_choose_action(message: types.Message):
await message.answer(text=f'Вы выбрали {message.text}')
await dp.current_state().reset_state() # Выход из состояния ввода даты
@dp.message_handler(state=state_time)
async def dp_choose_action(message: types.Message):
await message.answer(text='Ошибка ввода даты. Введите еще раз.', reply_markup=kb)
executor.start_polling(dp)
users = {
12398767: {'name': 'Vasya'},
98765467: {'name': 'Lena'}
}
def edit(user_id: int):
global users
users[user_id]['name'] = 'Petya'
def read(user_id: int):
print(users[user_id]['name'])
edit(12398767)
read(12398767) # Petya
print(users) # {12398767: {'name': 'Petya'}, 98765467: {'name': 'Lena'}}