mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 02:03:04 +00:00
Partial update examples.
This commit is contained in:
parent
b37763c23f
commit
06df482061
8 changed files with 24 additions and 91 deletions
|
|
@ -32,7 +32,7 @@ async def send_message(user_id: int, text: str) -> bool:
|
|||
:return:
|
||||
"""
|
||||
try:
|
||||
await bot.send_message(user_id, '<b>Hello, World!</b>')
|
||||
await bot.send_message(user_id, text)
|
||||
except exceptions.BotBlocked:
|
||||
log.error(f"Target [ID:{user_id}]: blocked by user")
|
||||
except exceptions.ChatNotFound:
|
||||
|
|
|
|||
|
|
@ -5,18 +5,14 @@ Babel is required.
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.types import ParseMode
|
||||
from aiogram.utils.executor import start_polling
|
||||
from aiogram.utils.markdown import *
|
||||
from aiogram import Bot, Dispatcher, executor, md, types
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
bot = Bot(token=API_TOKEN, loop=loop)
|
||||
bot = Bot(token=API_TOKEN, loop=loop, parse_mode=types.ParseMode.MARKDOWN)
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
|
|
@ -24,14 +20,14 @@ dp = Dispatcher(bot)
|
|||
async def check_language(message: types.Message):
|
||||
locale = message.from_user.locale
|
||||
|
||||
await message.reply(text(
|
||||
bold('Info about your language:'),
|
||||
text(' 🔸', bold('Code:'), italic(locale.locale)),
|
||||
text(' 🔸', bold('Territory:'), italic(locale.territory or 'Unknown')),
|
||||
text(' 🔸', bold('Language name:'), italic(locale.language_name)),
|
||||
text(' 🔸', bold('English language name:'), italic(locale.english_name)),
|
||||
sep='\n'), parse_mode=ParseMode.MARKDOWN)
|
||||
await message.reply(md.text(
|
||||
md.bold('Info about your language:'),
|
||||
md.text(' 🔸', md.bold('Code:'), md.italic(locale.locale)),
|
||||
md.text(' 🔸', md.bold('Territory:'), md.italic(locale.territory or 'Unknown')),
|
||||
md.text(' 🔸', md.bold('Language name:'), md.italic(locale.language_name)),
|
||||
md.text(' 🔸', md.bold('English language name:'), md.italic(locale.english_name)),
|
||||
sep='\n'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_polling(dp, loop=loop, skip_updates=True)
|
||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.utils.executor import start_polling
|
||||
from aiogram import Bot, types, Dispatcher, executor
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
|
|
@ -32,10 +30,4 @@ async def echo(message: types.Message):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_polling(dp, loop=loop, skip_updates=True)
|
||||
|
||||
# Also you can use another execution method
|
||||
# >>> try:
|
||||
# >>> loop.run_until_complete(main())
|
||||
# >>> except KeyboardInterrupt:
|
||||
# >>> loop.stop()
|
||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
||||
|
|
|
|||
|
|
@ -1,47 +0,0 @@
|
|||
from aiogram import Bot, types
|
||||
from aiogram.contrib.middlewares.context import ContextMiddleware
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.types import ParseMode
|
||||
from aiogram.utils import markdown as md
|
||||
from aiogram.utils.executor import start_polling
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
# Setup Context middleware
|
||||
data: ContextMiddleware = dp.middleware.setup(ContextMiddleware())
|
||||
|
||||
|
||||
# Write custom filter
|
||||
async def demo_filter(message: types.Message):
|
||||
# Store some data in context
|
||||
command = data['command'] = message.get_command() or ''
|
||||
args = data['args'] = message.get_args() or ''
|
||||
data['has_args'] = bool(args)
|
||||
data['some_random_data'] = 42
|
||||
return command != '/bad_command'
|
||||
|
||||
|
||||
@dp.message_handler(demo_filter)
|
||||
async def send_welcome(message: types.Message):
|
||||
# Get data from context
|
||||
# All of this is available only in current context and from current update object
|
||||
# `data`- pseudo-alias for `ctx.get_update().conf['_context_data']`
|
||||
command = data['command']
|
||||
args = data['args']
|
||||
rand = data['some_random_data']
|
||||
has_args = data['has_args']
|
||||
|
||||
# Send as pre-formatted code block.
|
||||
await message.reply(md.hpre(f"""command: {command}
|
||||
args: {['Not available', 'available'][has_args]}: {args}
|
||||
some random data: {rand}
|
||||
message ID: {message.message_id}
|
||||
message: {message.html_text}
|
||||
"""), parse_mode=ParseMode.HTML)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_polling(dp)
|
||||
|
|
@ -21,9 +21,8 @@ Step 5: When you change the code of your bot you need to update po & mo files.
|
|||
|
||||
from pathlib import Path
|
||||
|
||||
from aiogram import Bot, Dispatcher, types
|
||||
from aiogram import Bot, Dispatcher, executor, types
|
||||
from aiogram.contrib.middlewares.i18n import I18nMiddleware
|
||||
from aiogram.utils import executor
|
||||
|
||||
TOKEN = 'BOT TOKEN HERE'
|
||||
I18N_DOMAIN = 'mybot'
|
||||
|
|
|
|||
|
|
@ -1,9 +1,7 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.utils.executor import start_polling
|
||||
from aiogram import Bot, types, Dispatcher, executor
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
|
|
@ -23,4 +21,4 @@ async def inline_echo(inline_query: types.InlineQuery):
|
|||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_polling(dp, loop=loop, skip_updates=True)
|
||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
||||
|
|
|
|||
|
|
@ -1,9 +1,6 @@
|
|||
import asyncio
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.types import ChatActions
|
||||
from aiogram.utils.executor import start_polling
|
||||
from aiogram import Bot, Dispatcher, executor, filters, types
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
|
|
@ -12,7 +9,7 @@ bot = Bot(token=API_TOKEN, loop=loop)
|
|||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
@dp.message_handler(commands=['start'])
|
||||
@dp.message_handler(filters.CommandStart())
|
||||
async def send_welcome(message: types.Message):
|
||||
# So... At first I want to send something like this:
|
||||
await message.reply("Do you want to see many pussies? Are you ready?")
|
||||
|
|
@ -21,7 +18,7 @@ async def send_welcome(message: types.Message):
|
|||
await asyncio.sleep(1)
|
||||
|
||||
# Good bots should send chat actions. Or not.
|
||||
await ChatActions.upload_photo()
|
||||
await types.ChatActions.upload_photo()
|
||||
|
||||
# Create media group
|
||||
media = types.MediaGroup()
|
||||
|
|
@ -39,9 +36,8 @@ async def send_welcome(message: types.Message):
|
|||
# media.attach_photo('<file_id>', 'cat-cat-cat.')
|
||||
|
||||
# Done! Send media group
|
||||
await bot.send_media_group(message.chat.id, media=media,
|
||||
reply_to_message_id=message.message_id)
|
||||
await message.reply_media_group(media=media)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
start_polling(dp, loop=loop, skip_updates=True)
|
||||
executor.start_polling(dp, loop=loop, skip_updates=True)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
import asyncio
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram import Bot, Dispatcher, executor, types
|
||||
from aiogram.contrib.fsm_storage.redis import RedisStorage2
|
||||
from aiogram.dispatcher import CancelHandler, DEFAULT_RATE_LIMIT, Dispatcher
|
||||
from aiogram.dispatcher import DEFAULT_RATE_LIMIT
|
||||
from aiogram.dispatcher.handler import CancelHandler
|
||||
from aiogram.dispatcher.middlewares import BaseMiddleware
|
||||
from aiogram.utils import executor
|
||||
from aiogram.utils.exceptions import Throttled
|
||||
|
||||
TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue