Name | ttbotlib JSON |
Version |
0.13
JSON |
| download |
home_page | None |
Summary | pararam.io bot framework |
upload_time | 2024-11-13 10:37:05 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.10 |
license | MIT License |
keywords |
pararam.io
async
bot
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
## INSTALL
Install bot-server based on ASGI API and ASGI-base webserver (for example https://www.uvicorn.org/)
```
$ pip install ttbot
$ pip install uvicorn
```
## CODE
Create `bot.py`
```python
from ttbot.server import Action, Post, Request, Server
class MyBot(Server):
async def message_handler(self, post: Post) -> str:
''' Called when bot mentioned in post, return text will be published as reply post '''
post.user_id # int, who send post
post.chat_id # int, in what chat
post.post_no # int, post number
post.text # str, post text
post.team_id # int, if chat from team
post.reply_no # int, if post has reply to other post (number)
post.reply_text # str, if post has reply to other post (text)
return f'Received {post}'
async def action_handler(self, action: Action) -> dict:
''' Called when user clicks on bot action link or button '''
action.action # str, action name
action.params # dict, params of action
action.post # Post, post which attached action
action.user_id # int, who send post
action.post.user_id # 0
action.post.chat_id # int, in what chat
action.post.post_no # int, post number
action.post.text # ''
action.post.team_id # int, if chat from team
return {'action': str(action)}
async def api_handler(self, request: Request) -> dict:
''' Web server for bot local aims '''
request.method # str, HTTP method
request.path # str, HTTP path
request.query # dict, parsed HTTP url query string
request.headers # dict, parsed HTTP headers
request.data # dict, parsed json from HTTP body
return {'request': str(request)}
app = MyBot()
```
## RUN
SECRET given when bot created
```
SECRET=123 uvicorn example.app:app
SECRET=123 API_HOST=ruapi.pararam.io uvicorn example.app:app
```
## API
АПИ для получения сообщений боту (подписан pararam)
```
curl -XPOST -d '{"user_id": 1, "chat_id": 2, "post_no": 3, "text": "text"}' 'http://localhost:8000/bot'
```
АПИ действий бота (для кнопок, подписан pararam)
```
curl -XPOST -d '{"user_id": 1, "chat_id": 2, "post_no": 3, "action": "submit", "params": {}}' 'http://localhost:8000/act'
```
Собственное АПИ бота для не связанных с pararam операций
```
curl -XPOST -d '{"a":1}' 'http://localhost:8000/api?s=1&s=2'
```
Raw data
{
"_id": null,
"home_page": null,
"name": "ttbotlib",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "pararam.io, async, bot",
"author": null,
"author_email": "Tamtam team <support@tamtamteam.com>",
"download_url": "https://files.pythonhosted.org/packages/fb/49/6f63fd9f096051dd3052f7b08a3b9cd1675c18cc7d775d3748d0c75517ff/ttbotlib-0.13.tar.gz",
"platform": null,
"description": "## INSTALL\n\nInstall bot-server based on ASGI API and ASGI-base webserver (for example https://www.uvicorn.org/)\n\n```\n$ pip install ttbot\n$ pip install uvicorn \n```\n\n\n## CODE\n\nCreate `bot.py`\n\n```python\nfrom ttbot.server import Action, Post, Request, Server\n\n\nclass MyBot(Server):\n async def message_handler(self, post: Post) -> str:\n ''' Called when bot mentioned in post, return text will be published as reply post '''\n post.user_id # int, who send post\n post.chat_id # int, in what chat\n post.post_no # int, post number\n post.text # str, post text\n post.team_id # int, if chat from team\n post.reply_no # int, if post has reply to other post (number)\n post.reply_text # str, if post has reply to other post (text)\n return f'Received {post}'\n\n async def action_handler(self, action: Action) -> dict:\n ''' Called when user clicks on bot action link or button '''\n action.action # str, action name\n action.params # dict, params of action\n action.post # Post, post which attached action\n action.user_id # int, who send post\n action.post.user_id # 0\n action.post.chat_id # int, in what chat\n action.post.post_no # int, post number\n action.post.text # ''\n action.post.team_id # int, if chat from team\n return {'action': str(action)}\n\n async def api_handler(self, request: Request) -> dict:\n ''' Web server for bot local aims '''\n request.method # str, HTTP method\n request.path # str, HTTP path\n request.query # dict, parsed HTTP url query string\n request.headers # dict, parsed HTTP headers\n request.data # dict, parsed json from HTTP body\n return {'request': str(request)}\n\n\napp = MyBot()\n```\n\n\n## RUN\n\nSECRET given when bot created\n\n```\nSECRET=123 uvicorn example.app:app\nSECRET=123 API_HOST=ruapi.pararam.io uvicorn example.app:app\n```\n\n\n## API\n\n\u0410\u041f\u0418 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u043e\u0442\u0443 (\u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d pararam)\n\n```\ncurl -XPOST -d '{\"user_id\": 1, \"chat_id\": 2, \"post_no\": 3, \"text\": \"text\"}' 'http://localhost:8000/bot'\n```\n\n\u0410\u041f\u0418 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0431\u043e\u0442\u0430 (\u0434\u043b\u044f \u043a\u043d\u043e\u043f\u043e\u043a, \u043f\u043e\u0434\u043f\u0438\u0441\u0430\u043d pararam)\n\n```\ncurl -XPOST -d '{\"user_id\": 1, \"chat_id\": 2, \"post_no\": 3, \"action\": \"submit\", \"params\": {}}' 'http://localhost:8000/act'\n```\n\n\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0410\u041f\u0418 \u0431\u043e\u0442\u0430 \u0434\u043b\u044f \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 pararam \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439\n\n```\ncurl -XPOST -d '{\"a\":1}' 'http://localhost:8000/api?s=1&s=2'\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "pararam.io bot framework",
"version": "0.13",
"project_urls": null,
"split_keywords": [
"pararam.io",
" async",
" bot"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "88d21b95c946586c3843552716bfef85da93660c7c848c95623ff3f9a8acac9b",
"md5": "f0f6270c6a34f698f1bf1b7fbf037906",
"sha256": "d3f319344277eb5f6e1c62df0a8d497da49cae2237185b259f753d36039b128f"
},
"downloads": -1,
"filename": "ttbotlib-0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f0f6270c6a34f698f1bf1b7fbf037906",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 8820,
"upload_time": "2024-11-13T10:37:03",
"upload_time_iso_8601": "2024-11-13T10:37:03.836437Z",
"url": "https://files.pythonhosted.org/packages/88/d2/1b95c946586c3843552716bfef85da93660c7c848c95623ff3f9a8acac9b/ttbotlib-0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fb496f63fd9f096051dd3052f7b08a3b9cd1675c18cc7d775d3748d0c75517ff",
"md5": "ade6f5d347421d4f5479fbb8ff58188f",
"sha256": "a39a1733a25df2a6de2bc1be46f66f640b93afdff8d2517a9e3785ec2be27405"
},
"downloads": -1,
"filename": "ttbotlib-0.13.tar.gz",
"has_sig": false,
"md5_digest": "ade6f5d347421d4f5479fbb8ff58188f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 12169,
"upload_time": "2024-11-13T10:37:05",
"upload_time_iso_8601": "2024-11-13T10:37:05.642947Z",
"url": "https://files.pythonhosted.org/packages/fb/49/6f63fd9f096051dd3052f7b08a3b9cd1675c18cc7d775d3748d0c75517ff/ttbotlib-0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-13 10:37:05",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "ttbotlib"
}