Description
Here you will find files of an example inline telegram bot and everything you need to know about its guts.
You can try out my telegram bot by this link:
@joker_gut_bot Functionality Supported commands:
/help - it displays info about each command/startapp - it displays a single message@joker_gut_bot greeting (inline) - give you a choice between 3 greetings@joker_gut_bot memes (inline) - give you a choice between 3 memesSource code Here you can find a source code of an inline telegram bot.
main.py file
#!/usr/bin/env python
import asyncio
import logging
import sys
import uuid
from aiogram import F
from aiogram.types import Message, InlineQuery, InlineQueryResultArticle, InputTextMessageContent, InlineQueryResultPhoto
from aiogram.filters import Command
from aiogram.utils.i18n import gettext as _
from config import bot_dispatcher, bot
@bot_dispatcher.inline_query(F.query == "greeting")
async def send_greetings(inline_query: InlineQuery):
results = []
results.append(InlineQueryResultArticle(
id=str(uuid.uuid4()),
title=_("Обычное приветствие"),
input_message_content=InputTextMessageContent(
disable_web_page_preview=True,
message_text=_("Приветствую я вас сегодня.")
)
))
results.append(InlineQueryResultArticle(
id=str(uuid.uuid4()),
title=_("Жёсткое приветствие"),
input_message_content=InputTextMessageContent(
disable_web_page_preview=True,
message_text=_("Ну чё, как дела")
)
))
results.append(InlineQueryResultArticle(
id=str(uuid.uuid4()),
title=_("Спокойное приветствие"),
input_message_content=InputTextMessageContent(
disable_web_page_preview=True,
message_text=_("Привет")
)
))
await inline_query.answer(results)
@bot_dispatcher.inline_query(F.query == "memes")
async def send_user_images(inline_query: InlineQuery):
results = []
results.append(InlineQueryResultPhoto(
id=str(uuid.uuid4()),
photo_url="https://wisconsinskydivingcenter.com/wp-content/uploads/2024/05/you-dont-need-a-parachute-to-go-skydiving-meme.jpeg",
thumbnail_url="https://wisconsinskydivingcenter.com/wp-content/uploads/2024/05/you-dont-need-a-parachute-to-go-skydiving-meme.jpeg"
))
results.append(InlineQueryResultPhoto(
id=str(uuid.uuid4()),
photo_url="https://jungleroots.com/wp-content/uploads/2022/11/1_OkVxoXBTygSKB8K-zbB7uQ-300x176.jpeg",
thumbnail_url="https://jungleroots.com/wp-content/uploads/2022/11/1_OkVxoXBTygSKB8K-zbB7uQ-300x176.jpeg"
))
results.append(InlineQueryResultPhoto(
id=str(uuid.uuid4()),
photo_url="https://livestorm.imgix.net/1127/1651607695-obi-wan-alarm-work-meme.jpeg?h=auto&w=730&fm=jpeg&auto=format&q=90&dpr=1",
thumbnail_url="https://livestorm.imgix.net/1127/1651607695-obi-wan-alarm-work-meme.jpeg?h=auto&w=730&fm=jpeg&auto=format&q=90&dpr=1"
))
await inline_query.answer(results)
@bot_dispatcher.message(Command('help'))
async def help(message: Message):
await message.answer(_("Этот бот является примером того, как можно реализовать inline функционал для него."))
await message.answer(_("/startapp - запустить приложение"))
await message.answer(_("@joker_gut_bot memes - запросить мемы для показа"))
await message.answer(_("@joker_gut_bot greeting - запросить ответы по умолчанию"))
@bot_dispatcher.message(Command("startapp"))
async def start_app(message: Message):
await message.answer(_("Этот бот является примером того, как можно реализовать inline функционал для него."))
async def main() -> None:
await bot_dispatcher.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
config.py file
from aiogram import Bot, Dispatcher
from aiogram.utils.i18n import I18n, SimpleI18nMiddleware
with open(".env", "r") as file:
buffer = file.read()
line_pos = buffer.find("BOT_TOKEN")
TOKEN = buffer[buffer.find("=", line_pos) + 1:]
bot = Bot(TOKEN)
bot_dispatcher = Dispatcher(bot=bot)
# Set up translation
i18n = I18n(path="locales", domain="messages")
i18n_handler = SimpleI18nMiddleware(i18n)
i18n_handler.setup(bot_dispatcher)
Reviews
(0)