From 35ee8b0482c1f06ab78ff716cac0b6bc0b74848c Mon Sep 17 00:00:00 2001 From: Sidnev Nikolaj Date: Mon, 12 Aug 2019 16:45:14 +0300 Subject: [PATCH] add di in changelog --- README.md | 59 +++++++++-------------------------------------- docs/changelog.md | 1 + 2 files changed, 12 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 01df3156..42043f1e 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,9 @@ A little python library for building bots for Express

+ + Travis-CI + License @@ -24,7 +27,7 @@ Main features: * Simple integration with your web apps. - * Synchronous API as well as asynchronous. + * Asynchronous API with synchronous as a fallback option. * 100% test coverage. * 100% type annotated codebase. @@ -36,10 +39,9 @@ Python 3.6+ `pybotx` use the following libraries: -* Pydantic for the data parts. -* Requests for making synchronous calls to BotX API. -* Aiohttp for making asynchronous calls to BotX API. -* Aiojobs for dispatching asynchronous tasks. +* pydantic for the data parts. +* httpx for making HTTP calls to BotX API. +* loguru for beautiful and powerful logs. ## Installation ```bash @@ -58,48 +60,13 @@ Let's create a simple echo bot. * Create a file `main.py` with following content: ```Python3 -from botx import Bot, Message, Status +from botx import Bot, CTS, Message, Status from fastapi import FastAPI from starlette.middleware.cors import CORSMiddleware from starlette.status import HTTP_202_ACCEPTED -bot = Bot(disable_credentials=True) - - -@bot.default_handler -def echo_handler(message: Message, bot: Bot): - bot.answer_message(message.body, message) - - -app = FastAPI() -app.add_middleware( - CORSMiddleware, - allow_origins=["*"], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - -@app.get("/status", response_model=Status) -def bot_status(): - return bot.status - - -@app.post("/command", status_code=HTTP_202_ACCEPTED) -def bot_command(message: Message): - bot.execute_command(message.dict()) -``` -

-Or use async def - -```Python3 hl_lines="1 6 10 11 23 24 28 33 34" -from botx import AsyncBot, Message, Status -from fastapi import FastAPI -from starlette.middleware.cors import CORSMiddleware -from starlette.status import HTTP_202_ACCEPTED - -bot = AsyncBot(disable_credentials=True) +bot = Bot() +bot.add_cts(CTS(host="cts.example.com", secret_key="secret")) @bot.default_handler @@ -116,20 +83,16 @@ app.add_middleware( allow_headers=["*"], ) -app.add_event_handler("startup", bot.start) -app.add_event_handler("shutdown", bot.stop) - @app.get("/status", response_model=Status) async def bot_status(): - return bot.status + return await bot.status() @app.post("/command", status_code=HTTP_202_ACCEPTED) async def bot_command(message: Message): await bot.execute_command(message.dict()) ``` -
* Deploy a bot on your server using uvicorn and set the url for the webhook in Express. ```bash diff --git a/docs/changelog.md b/docs/changelog.md index 66ee5f63..4b69c11d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,7 @@ depending on different conditions defined in the handlers (to be added to future * Added mechanism for catching exceptions. * Changed methods signatures. See [api-reference](/api-reference/bots/) for details. * Fixed `opts` shape. +* Added dependency injection system ## 0.11.3