Telegram Bot framework
Project description
# telegraph_commander
Telegram simple bot framework
## 1. First, extend BaseBot class, and provide router and configuration:
```python
from telegraph_commander.logger import get_console_handler
from telegraph_commander.bot import BaseBot
from telegraph_commander.command import CommandRouter, BotCommand
from telegraph_commander.config import BotConfig
class YourBotConfig(BotConfig):
TELEGRAM_API_KEY = '<api_key>'
DEFAULT_COMMAND = 'menu'
class YourBot(BaseBot):
router_class = CommandRouter
config_class = YourBotConfig
bot = YourBot()
```
## 2. Bind commands to router :
```python
@bot.router.command('menu')
class MenuCommand(BotCommand):
async def run(self):
await self.telegram_api.send_message(self.chat_id, 'Select action', variants=[['/mul']])
@bot.router.command('mul')
class MulCommand(BotCommand):
PARAMS = (
dict(name='operand_1', title='first argument'),
dict(name='operand_2', title='second argument')
)
async def run_handle(self):
result = float(self.operand_1) * float(self.operand_2)
await self.telegram_api.send_message(self.chat_id, 'result: {}'.format(result))
```
## 3. Add console logger handler(optional), and run your bot:
```python
bot.logger.addHandler(get_console_handler())
bot.run()
```
Telegram simple bot framework
## 1. First, extend BaseBot class, and provide router and configuration:
```python
from telegraph_commander.logger import get_console_handler
from telegraph_commander.bot import BaseBot
from telegraph_commander.command import CommandRouter, BotCommand
from telegraph_commander.config import BotConfig
class YourBotConfig(BotConfig):
TELEGRAM_API_KEY = '<api_key>'
DEFAULT_COMMAND = 'menu'
class YourBot(BaseBot):
router_class = CommandRouter
config_class = YourBotConfig
bot = YourBot()
```
## 2. Bind commands to router :
```python
@bot.router.command('menu')
class MenuCommand(BotCommand):
async def run(self):
await self.telegram_api.send_message(self.chat_id, 'Select action', variants=[['/mul']])
@bot.router.command('mul')
class MulCommand(BotCommand):
PARAMS = (
dict(name='operand_1', title='first argument'),
dict(name='operand_2', title='second argument')
)
async def run_handle(self):
result = float(self.operand_1) * float(self.operand_2)
await self.telegram_api.send_message(self.chat_id, 'result: {}'.format(result))
```
## 3. Add console logger handler(optional), and run your bot:
```python
bot.logger.addHandler(get_console_handler())
bot.run()
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
telegraph_commander-0.0.3.zip
(12.2 kB
view details)
File details
Details for the file telegraph_commander-0.0.3.zip.
File metadata
- Download URL: telegraph_commander-0.0.3.zip
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
643537f0ab6ffb7fff3fe60f91f06008e1847cdaef7d3e4445c5c9ec8acc760b
|
|
| MD5 |
586233c2c6c0801a1aa8c474c1c30298
|
|
| BLAKE2b-256 |
2c16d47cc989062a6bd4a655651b106e85c715cb106b3a9e34369a6160f1780c
|