Эхо-бот
Самый простой пример. Бот отвечает тем же текстом.
Python (aiogram)
import asyncio, os
from aiogram import Bot, Dispatcher, F
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.client.telegram import TelegramAPIServer
from aiogram.filters import CommandStart
from aiogram.types import Message
session = AiohttpSession(api=TelegramAPIServer.from_base('https://api.telefon.chat'))
bot = Bot(token=os.environ['BOT_TOKEN'], session=session)
dp = Dispatcher()
@dp.message(CommandStart())
async def start(m: Message):
await m.answer('Привет!')
@dp.message(F.text)
async def echo(m: Message):
await m.answer(m.text)
if __name__ == '__main__':
asyncio.run(dp.start_polling(bot))
Запуск:
pip install aiogram
BOT_TOKEN=ваш_токен python bot.py
Node.js (grammy)
import { Bot } from 'grammy';
const bot = new Bot(process.env.BOT_TOKEN!, {
client: { apiRoot: 'https://api.telefon.chat' }
});
bot.command('start', (ctx) => ctx.reply('Привет!'));
bot.on('message:text', (ctx) => ctx.reply(ctx.message.text));
bot.start();
Запуск:
npm install grammy
BOT_TOKEN=ваш_токен npx ts-node bot.ts
Что делать дальше
- Добавьте обработку команд (
/help,/info) - Подключите inline-клавиатуру
- Добавьте отправку медиа: send-media
- Перейдите на webhook: Webhooks
Примечание про имена:
TelegramAPIServer— это название класса внутри библиотеки aiogram, параметризующего базовый URL HTTP-клиента. Достаточно передать наш URL — никакая логика к чужому сервису не подмешивается.