-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
109 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# pybotx | ||
|
||
*A python library for building bots and smartapps for eXpress messenger.* | ||
|
||
[](https://badge.fury.io/py/botx) | ||
 | ||
[](https://codecov.io/gh/ExpressApp/pybotx/branch/next) | ||
[](https://github.com/ambv/black) | ||
|
||
|
||
## Features | ||
|
||
* Designed to be ease to use | ||
* Simple integration with async web-frameworks | ||
* Support middlewares for command, command-collector and bot | ||
* 100% test coverage | ||
* 100% type annotated codebase | ||
|
||
|
||
## Documentation | ||
|
||
*TODO: Add link* | ||
*TODO: Note that docs available only in Russian* | ||
|
||
|
||
## Installation | ||
|
||
Install pybotx using `pip`: | ||
|
||
```bash | ||
pip install git+https://github.com/ExpressApp/pybotx.git@next | ||
``` | ||
|
||
*TODO: Add not about unstable version* | ||
|
||
|
||
## Usage example | ||
|
||
```python | ||
from http import HTTPStatus | ||
from uuid import UUID | ||
|
||
from botx import ( | ||
Bot, | ||
BotAccountWithSecret, | ||
ChatCreatedEvent, | ||
HandlerCollector, | ||
IncomingMessage, | ||
build_command_accepted_response, | ||
) | ||
from fastapi import FastAPI, Request | ||
from fastapi.responses import JSONResponse | ||
|
||
collector = HandlerCollector() | ||
|
||
|
||
@collector.chat_created | ||
async def chat_created_handler(event: ChatCreatedEvent, bot: Bot) -> None: | ||
await bot.answer_message("Hello!") | ||
|
||
|
||
@collector.command("/echo", description="Send back the received message body") | ||
async def echo_handler(message: IncomingMessage, bot: Bot) -> None: | ||
await bot.answer_message(message.body) | ||
|
||
|
||
@collector.default_message_handler | ||
async def default_message_handler(event: IncomingMessage, bot: Bot) -> None: | ||
await bot.answer_message("Sorry, command not found.") | ||
|
||
|
||
bot = Bot( | ||
collectors=[collector], | ||
bot_accounts=[ | ||
BotAccountWithSecret( # Replace fake account credentials with yours | ||
id=UUID("123e4567-e89b-12d3-a456-426655440000"), | ||
host="cts.example.com", | ||
secret_key="e29b417773f2feab9dac143ee3da20c5", | ||
) | ||
], | ||
) | ||
|
||
app = FastAPI() | ||
app.add_event_handler("startup", bot.startup) | ||
app.add_event_handler("shutdown", bot.shutdown) | ||
|
||
|
||
@app.post("/command") | ||
async def command_handler(request: Request) -> JSONResponse: | ||
bot.async_execute_raw_bot_command(await request.json()) | ||
return JSONResponse( | ||
build_command_accepted_response(), status_code=HTTPStatus.ACCEPTED | ||
) | ||
|
||
|
||
@app.get("/status") | ||
async def status_handler(request: Request) -> JSONResponse: | ||
status = await bot.raw_get_status(dict(request.query_params)) | ||
return JSONResponse(status) | ||
|
||
|
||
@app.post("/notification/callback") | ||
async def callback_handler(request: Request) -> JSONResponse: | ||
bot.set_raw_botx_method_result(await request.json()) | ||
return JSONResponse( | ||
build_command_accepted_response(), | ||
status_code=HTTPStatus.ACCEPTED, | ||
) | ||
``` |
b399da4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Deployed on https://61e91aab6eb458d7b1c6cc72--pybotx.netlify.app