Skip to content

Commit

Permalink
Merge branch 'develop': v 1.1.6. Add errors handler and push aioAlice…
Browse files Browse the repository at this point in the history
… to pypi
  • Loading branch information
mahenzon committed Aug 14, 2018
2 parents 987314e + 91ea575 commit e285116
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>


# aioalice
# aioAlice

## AsyncIO library for Yandex Alice (Yandex Dialogs)

Expand All @@ -26,9 +26,11 @@ source ./aliceenv/bin/activate

pip install pip -U
pip install setuptools -U
pip install uvloop # uvloop if you want
pip install uvloop # uvloop if you want, but it can cause some problems

pip install git+https://github.com/surik00/aioalice.git -U
pip install aioalice -U
# Or install from GitHub:
# pip install git+https://github.com/surik00/aioalice.git -U

# or if you don't have git installed:
# 1. download ZIP
Expand Down
62 changes: 62 additions & 0 deletions README-pypa.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# aioAlice

## AsyncIO library for Yandex Alice (Yandex Dialogs)


## Why?
- Work with classes, don't bother parsing JSON
- Auto answer to webhook even if you were not fast enough to create answer - there won't be a server error, but you'll get a log
> Auto response will not help if you are not using async IO. So consider not to use any long processing synchronous tasks inside handlers
- Handy handlers to match incoming commands
- Finite-State Machine
- Easy images upload, easy answers generation


### Installation

```bash
# make sure you use virtual env and python 3.6+:
python3.6 -m venv aliceenv
source ./aliceenv/bin/activate

pip install pip -U
pip install setuptools -U
pip install uvloop # uvloop if you want

pip install aioalice -U
# Or install from GitHub:
# pip install git+https://github.com/surik00/aioalice.git -U

# or if you don't have git installed:
# 1. download ZIP
# 2. unarchive and go to dir
# 3. run:
python setup.py install
```


### Quick start

[Hello alice](https://github.com/surik00/aioalice/blob/master/examples/hello-alice.py)

```python
dp = Dispatcher()

@dp.request_handler()
async def handle_all_requests(alice_request):
return alice_request.response('Hello world!')
```


### Cards

- [All examples](https://github.com/surik00/aioalice/blob/master/examples/README-en.md)

- [Upload image example](https://github.com/surik00/aioalice/blob/master/examples/upload_image.py)
- [Big Image Card example](https://github.com/surik00/aioalice/blob/master/examples/card_big_image.py)
- [Items List Card example](https://github.com/surik00/aioalice/blob/master/examples/card_items_list.py)


### JSON serializing

If you want to use a faster json library, install [rapidjson](https://github.com/python-rapidjson/python-rapidjson) or [ujson](https://github.com/esnme/ultrajson), it will be detected and used automatically
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Яндекс Алиса. Диалоги (навыки)


**aioalice** это асинхронная библиотека для взаимодействия с Алисой
**aioAlice** это асинхронная библиотека для взаимодействия с Алисой


## Зачем?
Expand All @@ -28,7 +28,9 @@ pip install pip -U
pip install setuptools -U
pip install uvloop # uvloop при желании

pip install git+https://github.com/surik00/aioalice.git -U
pip install aioalice -U
# Or install from GitHub:
# pip install git+https://github.com/surik00/aioalice.git -U

# Если git не установлен:
# 1. скачайте ZIP
Expand Down
2 changes: 1 addition & 1 deletion aioalice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())


__version__ = '1.1.5'
__version__ = '1.1.6'
1 change: 1 addition & 0 deletions examples/README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Buy elephant](buy-elephant.py)
- [FSM example \(Finite State Machine\)](FSM_games.py)
- [Skip Handlers, log requests](skip_handler_log_everything.py)
- [Handle errors](handle-errors.py)
- [Get uploaded images](get_images.py)
- [Upload an image](upload_image.py)
- [Get quota status and delete an image](quota_status_and_delete_image.py)
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Купи слона](buy-elephant.py)
- [Работа с FSM на примере игры \(Finite State Machine - Машина состояний\)](FSM_games.py)
- [Пропускаем хэндлеры, логгируем запросы](skip_handler_log_everything.py)
- [Ловим ошибки](handle-errors.py)
- [Проверить загруженные изображения](get_images.py)
- [Загрузить изображение](upload_image.py)
- [Проверить занятое место и удалить изображение](quota_status_and_delete_image.py)
Expand Down
36 changes: 36 additions & 0 deletions examples/handle-errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import logging

from aiohttp import web
from aioalice import Dispatcher, get_new_configured_app


logging.basicConfig(format=u'%(filename)s [LINE:%(lineno)d] #%(levelname)-8s [%(asctime)s] %(message)s',
level=logging.INFO)


WEBHOOK_URL_PATH = '/my-alice-webhook/' # webhook endpoint

WEBAPP_HOST = 'localhost'
WEBAPP_PORT = 3001

dp = Dispatcher()


@dp.request_handler()
async def handle_all_requests(alice_request):
1 / 0 # some unexpected error
return alice_request.response('Hello world!')


# if any error inside handler occur
@dp.errors_handler()
async def the_only_errors_handler(alice_request, e):
# Log the error
logging.error('An error!', exc_info=e)
# Return answer so API doesn't consider your skill non-working
return alice_request.response('Oops! There was an error!')


if __name__ == '__main__':
app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_URL_PATH)
web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
# Check python version
MINIMAL_PY_VERSION = (3, 6)
if sys.version_info < MINIMAL_PY_VERSION:
raise RuntimeError('aioalice works only with Python {}+'.format('.'.join(map(str, MINIMAL_PY_VERSION))))
raise RuntimeError('aioAlice works only with Python {}+'.format('.'.join(map(str, MINIMAL_PY_VERSION))))

__version__ = '1.1.5'
__version__ = '1.1.6'


def get_description():
"""
Read full description from 'README.md'
Read full description from 'README-pypa.md'
:return: description
:rtype: str
"""
with open('README.md', 'r', encoding='utf-8') as f:
with open('README-pypa.md', 'r', encoding='utf-8') as f:
return f.read()


Expand All @@ -47,7 +47,7 @@ def get_requirements(filename=None):


setup(
name='aioalice',
name='aioAlice',
version=__version__,
packages=find_packages(exclude=('tests', 'tests.*', 'examples',)),
url='https://github.com/surik00/aioalice',
Expand All @@ -57,6 +57,7 @@ def get_requirements(filename=None):
author_email='surenkhorenyan@gmail.com',
description='Asynchronous library for Yandex Dialogs (Alice) API',
long_description=get_description(),
long_description_content_type='text/markdown',
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
Expand Down

0 comments on commit e285116

Please sign in to comment.