Skip to content

Commit

Permalink
Recommend usage of type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
szastupov committed Jun 25, 2017
1 parent 6b84f2b commit c68e911
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,32 @@ Then you can create a new bot in few lines:

.. code:: python
from aiotg import Bot
from aiotg import Bot, Chat
bot = Bot(api_token="...")
@bot.command(r"/echo (.+)")
def echo(chat, match):
def echo(chat: Chat, match):
return chat.reply(match.group(1))
bot.run()
Now run it with a proper API\_TOKEN and it should reply to /echo commands.

.. note:: Type annotations are not required but will help you editor/IDE to provide code completion.

The example above looks like a normal synchronous code but it actually returns a coroutine.
If you want to make an external request (and that's what bots usually do) just use aiohttp and async/await syntax:

.. code:: python
import aiohttp
from aiotg import Bot
from aiotg import Bot, Chat
bot = Bot(api_token="...")
@bot.command("bitcoin")
async def bitcoin(chat, match):
async def bitcoin(chat: Chat, match):
url = "https://api.bitcoinaverage.com/ticker/global/USD/"
async with aiohttp.get(url) as s:
info = await s.json()
Expand Down
4 changes: 2 additions & 2 deletions examples/async.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from aiotg import Bot
from aiotg import Bot, Chat

bot = Bot(os.environ["API_TOKEN"])


@bot.command(r"bitcoin")
async def bitcoin(chat, match):
async def bitcoin(chat: Chat, match):
url = "https://api.bitcoinaverage.com/ticker/global/USD/"
async with bot.session.get(url) as s:
info = await s.json()
Expand Down
4 changes: 2 additions & 2 deletions examples/echo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from aiotg import Bot
from aiotg import Bot, Chat

bot = Bot(os.environ["API_TOKEN"])


@bot.command(r"/echo (.+)")
def echo(chat, match):
def echo(chat: Chat, match):
return chat.reply(match.group(1))


Expand Down

0 comments on commit c68e911

Please sign in to comment.