-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop': v 1.1.6. Add errors handler and push aioAlice…
… to pypi
- Loading branch information
Showing
8 changed files
with
116 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ | |
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | ||
|
||
|
||
__version__ = '1.1.5' | ||
__version__ = '1.1.6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters