Partial update examples.

This commit is contained in:
Alex Root Junior 2018-07-14 16:28:20 +03:00
parent b37763c23f
commit 06df482061
8 changed files with 24 additions and 91 deletions

View file

@ -32,7 +32,7 @@ async def send_message(user_id: int, text: str) -> bool:
:return: :return:
""" """
try: try:
await bot.send_message(user_id, '<b>Hello, World!</b>') await bot.send_message(user_id, text)
except exceptions.BotBlocked: except exceptions.BotBlocked:
log.error(f"Target [ID:{user_id}]: blocked by user") log.error(f"Target [ID:{user_id}]: blocked by user")
except exceptions.ChatNotFound: except exceptions.ChatNotFound:

View file

@ -5,18 +5,14 @@ Babel is required.
import asyncio import asyncio
import logging import logging
from aiogram import Bot, types from aiogram import Bot, Dispatcher, executor, md, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode
from aiogram.utils.executor import start_polling
from aiogram.utils.markdown import *
API_TOKEN = 'BOT TOKEN HERE' API_TOKEN = 'BOT TOKEN HERE'
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop() 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) dp = Dispatcher(bot)
@ -24,14 +20,14 @@ dp = Dispatcher(bot)
async def check_language(message: types.Message): async def check_language(message: types.Message):
locale = message.from_user.locale locale = message.from_user.locale
await message.reply(text( await message.reply(md.text(
bold('Info about your language:'), md.bold('Info about your language:'),
text(' 🔸', bold('Code:'), italic(locale.locale)), md.text(' 🔸', md.bold('Code:'), md.italic(locale.locale)),
text(' 🔸', bold('Territory:'), italic(locale.territory or 'Unknown')), md.text(' 🔸', md.bold('Territory:'), md.italic(locale.territory or 'Unknown')),
text(' 🔸', bold('Language name:'), italic(locale.language_name)), md.text(' 🔸', md.bold('Language name:'), md.italic(locale.language_name)),
text(' 🔸', bold('English language name:'), italic(locale.english_name)), md.text(' 🔸', md.bold('English language name:'), md.italic(locale.english_name)),
sep='\n'), parse_mode=ParseMode.MARKDOWN) sep='\n'))
if __name__ == '__main__': if __name__ == '__main__':
start_polling(dp, loop=loop, skip_updates=True) executor.start_polling(dp, loop=loop, skip_updates=True)

View file

@ -1,9 +1,7 @@
import asyncio import asyncio
import logging import logging
from aiogram import Bot, types from aiogram import Bot, types, Dispatcher, executor
from aiogram.dispatcher import Dispatcher
from aiogram.utils.executor import start_polling
API_TOKEN = 'BOT TOKEN HERE' API_TOKEN = 'BOT TOKEN HERE'
@ -32,10 +30,4 @@ async def echo(message: types.Message):
if __name__ == '__main__': if __name__ == '__main__':
start_polling(dp, loop=loop, skip_updates=True) executor.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()

View file

@ -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)

View file

@ -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 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.contrib.middlewares.i18n import I18nMiddleware
from aiogram.utils import executor
TOKEN = 'BOT TOKEN HERE' TOKEN = 'BOT TOKEN HERE'
I18N_DOMAIN = 'mybot' I18N_DOMAIN = 'mybot'

View file

@ -1,9 +1,7 @@
import asyncio import asyncio
import logging import logging
from aiogram import Bot, types from aiogram import Bot, types, Dispatcher, executor
from aiogram.dispatcher import Dispatcher
from aiogram.utils.executor import start_polling
API_TOKEN = 'BOT TOKEN HERE' API_TOKEN = 'BOT TOKEN HERE'
@ -23,4 +21,4 @@ async def inline_echo(inline_query: types.InlineQuery):
if __name__ == '__main__': if __name__ == '__main__':
start_polling(dp, loop=loop, skip_updates=True) executor.start_polling(dp, loop=loop, skip_updates=True)

View file

@ -1,9 +1,6 @@
import asyncio import asyncio
from aiogram import Bot, types from aiogram import Bot, Dispatcher, executor, filters, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ChatActions
from aiogram.utils.executor import start_polling
API_TOKEN = 'BOT TOKEN HERE' API_TOKEN = 'BOT TOKEN HERE'
@ -12,7 +9,7 @@ bot = Bot(token=API_TOKEN, loop=loop)
dp = Dispatcher(bot) dp = Dispatcher(bot)
@dp.message_handler(commands=['start']) @dp.message_handler(filters.CommandStart())
async def send_welcome(message: types.Message): async def send_welcome(message: types.Message):
# So... At first I want to send something like this: # So... At first I want to send something like this:
await message.reply("Do you want to see many pussies? Are you ready?") 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) await asyncio.sleep(1)
# Good bots should send chat actions. Or not. # Good bots should send chat actions. Or not.
await ChatActions.upload_photo() await types.ChatActions.upload_photo()
# Create media group # Create media group
media = types.MediaGroup() media = types.MediaGroup()
@ -39,9 +36,8 @@ async def send_welcome(message: types.Message):
# media.attach_photo('<file_id>', 'cat-cat-cat.') # media.attach_photo('<file_id>', 'cat-cat-cat.')
# Done! Send media group # Done! Send media group
await bot.send_media_group(message.chat.id, media=media, await message.reply_media_group(media=media)
reply_to_message_id=message.message_id)
if __name__ == '__main__': if __name__ == '__main__':
start_polling(dp, loop=loop, skip_updates=True) executor.start_polling(dp, loop=loop, skip_updates=True)

View file

@ -1,11 +1,10 @@
import asyncio import asyncio
from aiogram import Bot, types from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.redis import RedisStorage2 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.dispatcher.middlewares import BaseMiddleware
from aiogram.utils import executor
from aiogram.utils.exceptions import Throttled
TOKEN = 'BOT TOKEN HERE' TOKEN = 'BOT TOKEN HERE'