Skip to main content

pararam.io bot framework

Project description

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

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'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ttbotlib-0.12.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ttbotlib-0.12-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file ttbotlib-0.12.tar.gz.

File metadata

  • Download URL: ttbotlib-0.12.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for ttbotlib-0.12.tar.gz
Algorithm Hash digest
SHA256 7abd37494851ba1a188b01287b97a70a496ff171641aa27a97c4eda409826c43
MD5 9860808823dc1b6922aacc52eeaef257
BLAKE2b-256 d541a3f3ac56a678eb637cc8acc48c0e2c0e2f835700a26c9504fde2821d78e8

See more details on using hashes here.

File details

Details for the file ttbotlib-0.12-py3-none-any.whl.

File metadata

  • Download URL: ttbotlib-0.12-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for ttbotlib-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 05fcfa5bf8fc15b23e4b293081dac6902451e335069c15af92d0ce1cb90e502d
MD5 2a86663e49930929dd7acb29a179da89
BLAKE2b-256 a37754e4e9687a235146966d94f0c720ca41c515e17b6d23e69b6ab9ffedd043

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page