# [What is a Telegram Bot]
## Get a Telegram Bot API TOKEN:
* Open Telegram and search for ```@BotFather```
* Create a new bot by following the steps from the image:

## Setup
```bash
pip install DjTbot
python manage.py makemigrations
python manage.py migrate
```
## Run Bot
```bash
python manage.py runbot
```
## Create users & groups
```bash
python manage.py createsuperuser
python manage.py runserver
```
Create T bot groups and users:
browse to http://127.0.0.1:8000/tbot/
Users in group Admins can also manage groups and users via bot commands
# Create your own commands
1.- Start a new django app ```django-admin startapp myapp```
2.- Add your app to settings.INSTALLED_APPS (ProjectName/ProjectName/settings/base.py)
3.- Create a commands.py file inside your app
commands.py example:
```python
from DjTbot.commands import Command
class Example1Command(Command):
command = ['example', 'e'] # One command can be call with many names
help_text = 'Example command'
usage = '/example'
allowed_groups = ['mygroup']
def run(self, update, context):
context.bot.sendMessage(chat_id=update.message.chat_id, text='message')
self.send_ussage(context.bot, update)
self.admin_broadcast(context.bot, "Example command called")
```
*** Arguments sent to commands can be accessible through ```context.args``` or you can use the following code snippet:
#### Parsing arguments with TelegramArgumentParser
##### arguments.py
```python
from abc import ABCMeta, abstractmethod
from DjTbot.arguments import TelegramArgumentParser
class TBotArgumentParser(TelegramArgumentParser, metaclass=ABCMeta):
@abstractmethod
def add_arguments(self):
pass
@staticmethod
def factory(command_class_name):
parsers = {
'AddGroupCommand': TelegramIDGroupArgumentParser,
# Add here as many parsers as you need, key values must be the same as the command class name
}
return parsers[command_class_name]()
# All your Argument parsers must inherit form the one created above
class TelegramIDGroupArgumentParser(TBotArgumentParser):
def add_arguments(self):
# add_argument syntaxis from argparse https://docs.python.org/3/library/argparse.html
self.add_argument('telegram_id', type=int)
self.add_argument('group')
```
##### comands.py
```python
from DjTbot.utils import addgroup
from DjTbot.commands import CommandWithArgumentsMixin, Command
from DjTbot.arguments import TBotArgumentParser
class TBotCommandWithArgumentsMixin(CommandWithArgumentsMixin):
argument_parser_factory_class = TBotArgumentParser
class AddGroupCommand(TBotCommandWithArgumentsMixin, Command): # You must follow this order on inherited classes
command = ['addgroup']
help_text = 'Add user to group'
usage = '/addgroup telegram_id group_name'
allowed_groups = ['Admins']
def command_call(self, arguments, update, context):
try:
addgroup(arguments.telegram_id,
arguments.group) # Access your arguments with the given name in add_argument
except Exception as e:
self.admin_broadcast(context.bot, str(e))
self.send_ussage(context.bot, update)
```
4.- (optional) Create a listeners.py file inside your app if you wish to process messages. Example:
```python
import logging
from DjTbot.core.listeners import Listener, Filters
logger = logging.getLogger("DjTbot")
class LocationProcess(Listener):
allowed_groups = ['Admins']
filters = Filters.location
def run(self, update, context):
logger.debug(update)
a = update.message.location
context.bot.sendMessage(chat_id=update.message.chat_id, text="Hola {}\n Estas en\n Latitud {}\nLongitud: {}"
.format(update.message.from_user['first_name'], a['latitude'], a['longitude']))
class RepeatListener(Listener):
filters = Filters.text
def run(self, update, context):
context.bot.sendMessage(chat_id=update.message.chat_id, text=update.message.text)
```
# Management commands
There are also some management commands
addgroup
adduser
addusertogroup
clear_api_key
createbotadmin
lsgroups
lsusers
removeuserfromgroup
rmgroup
rmuser
runbot
sendmessage
## sendmessage
```bash
usage: manage.py sendmessage [-h]
(--user USER | --user-id USER_ID | --group GROUP | --all)
[--version] [-v {0,1,2,3}] [--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback]
[--no-color] [--force-color]
message
```
[PythonTelegram Bot Documentation](https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html)
[What is a Telegram Bot]:https://www.nobbot.com/redes/los-bots-telegram-conseguir-infinidad-funcionalidades-nuestras-conversaciones/
Raw data
{
"_id": null,
"home_page": "https://git.herrerosolis.com/bots/DjTbot",
"name": "DjTbot",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "Django,Telegram,Bot",
"author": "Rafael Herrero Solis",
"author_email": "rafahsolis@hotmail.com",
"download_url": "https://files.pythonhosted.org/packages/2e/c0/c6c9395efcf4a3aabb4d9bdd40e714664b814d11b09ff8db90f6835af7f3/DjTbot-0.0.7.tar.gz",
"platform": null,
"description": "# [What is a Telegram Bot]\n\n## Get a Telegram Bot API TOKEN:\n* Open Telegram and search for ```@BotFather```\n* Create a new bot by following the steps from the image:\n\n\n\n## Setup\n```bash\npip install DjTbot\npython manage.py makemigrations\npython manage.py migrate\n```\n\n## Run Bot\n```bash\npython manage.py runbot\n```\n\n## Create users & groups\n```bash\npython manage.py createsuperuser\npython manage.py runserver\n```\nCreate T bot groups and users: \nbrowse to http://127.0.0.1:8000/tbot/ \nUsers in group Admins can also manage groups and users via bot commands\n\n# Create your own commands\n1.- Start a new django app ```django-admin startapp myapp``` \n2.- Add your app to settings.INSTALLED_APPS (ProjectName/ProjectName/settings/base.py) \n3.- Create a commands.py file inside your app \n\ncommands.py example:\n```python\nfrom DjTbot.commands import Command\n\n\nclass Example1Command(Command):\n command = ['example', 'e'] # One command can be call with many names\n help_text = 'Example command'\n usage = '/example'\n allowed_groups = ['mygroup']\n\n def run(self, update, context):\n context.bot.sendMessage(chat_id=update.message.chat_id, text='message')\n self.send_ussage(context.bot, update)\n self.admin_broadcast(context.bot, \"Example command called\")\n```\n*** Arguments sent to commands can be accessible through ```context.args``` or you can use the following code snippet:\n#### Parsing arguments with TelegramArgumentParser\n##### arguments.py\n```python\nfrom abc import ABCMeta, abstractmethod\nfrom DjTbot.arguments import TelegramArgumentParser\n\n\nclass TBotArgumentParser(TelegramArgumentParser, metaclass=ABCMeta):\n @abstractmethod\n def add_arguments(self):\n pass\n\n @staticmethod\n def factory(command_class_name):\n parsers = {\n 'AddGroupCommand': TelegramIDGroupArgumentParser,\n # Add here as many parsers as you need, key values must be the same as the command class name\n }\n return parsers[command_class_name]()\n\n\n# All your Argument parsers must inherit form the one created above \nclass TelegramIDGroupArgumentParser(TBotArgumentParser):\n def add_arguments(self):\n # add_argument syntaxis from argparse https://docs.python.org/3/library/argparse.html\n self.add_argument('telegram_id', type=int)\n self.add_argument('group')\n```\n\n##### comands.py\n\n```python\nfrom DjTbot.utils import addgroup\nfrom DjTbot.commands import CommandWithArgumentsMixin, Command\nfrom DjTbot.arguments import TBotArgumentParser\n\n\nclass TBotCommandWithArgumentsMixin(CommandWithArgumentsMixin):\n argument_parser_factory_class = TBotArgumentParser\n\n\nclass AddGroupCommand(TBotCommandWithArgumentsMixin, Command): # You must follow this order on inherited classes\n command = ['addgroup']\n help_text = 'Add user to group'\n usage = '/addgroup telegram_id group_name'\n allowed_groups = ['Admins']\n\n def command_call(self, arguments, update, context):\n try:\n addgroup(arguments.telegram_id,\n arguments.group) # Access your arguments with the given name in add_argument\n except Exception as e:\n self.admin_broadcast(context.bot, str(e))\n self.send_ussage(context.bot, update)\n```\n\n4.- (optional) Create a listeners.py file inside your app if you wish to process messages. Example:\n```python\nimport logging\nfrom DjTbot.core.listeners import Listener, Filters\n\n\nlogger = logging.getLogger(\"DjTbot\")\n\n\nclass LocationProcess(Listener):\n allowed_groups = ['Admins']\n filters = Filters.location\n\n def run(self, update, context):\n logger.debug(update)\n a = update.message.location\n context.bot.sendMessage(chat_id=update.message.chat_id, text=\"Hola {}\\n Estas en\\n Latitud {}\\nLongitud: {}\"\n .format(update.message.from_user['first_name'], a['latitude'], a['longitude']))\n\n\nclass RepeatListener(Listener):\n filters = Filters.text\n\n def run(self, update, context):\n context.bot.sendMessage(chat_id=update.message.chat_id, text=update.message.text)\n```\n\n# Management commands\nThere are also some management commands\n\n addgroup\n adduser\n addusertogroup\n clear_api_key\n createbotadmin\n lsgroups\n lsusers\n removeuserfromgroup\n rmgroup\n rmuser\n runbot\n sendmessage\n\n\n## sendmessage\n```bash\nusage: manage.py sendmessage [-h]\n (--user USER | --user-id USER_ID | --group GROUP | --all)\n [--version] [-v {0,1,2,3}] [--settings SETTINGS]\n [--pythonpath PYTHONPATH] [--traceback]\n [--no-color] [--force-color]\n message\n```\n[PythonTelegram Bot Documentation](https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html) \n\n[What is a Telegram Bot]:https://www.nobbot.com/redes/los-bots-telegram-conseguir-infinidad-funcionalidades-nuestras-conversaciones/\n",
"bugtrack_url": null,
"license": "License :: OSI Approved :: MIT License",
"summary": "Django Telegram Bot",
"version": "0.0.7",
"project_urls": {
"Download": "https://git.herrerosolis.com/bots/DjTbot/-/archive/master/DjTbot-master.tar.gz",
"Homepage": "https://git.herrerosolis.com/bots/DjTbot"
},
"split_keywords": [
"django",
"telegram",
"bot"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6df477f0836c3bda055e35851a22be3bafeb9068c2bb19d011d20631b4d4ad5d",
"md5": "8433098cac52849f7937cc7c09aab047",
"sha256": "526266b5980002c903f15299da832392cfc1236d2ce17060a8f836e1a55ef3f0"
},
"downloads": -1,
"filename": "DjTbot-0.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8433098cac52849f7937cc7c09aab047",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 18634,
"upload_time": "2023-07-29T22:08:01",
"upload_time_iso_8601": "2023-07-29T22:08:01.860379Z",
"url": "https://files.pythonhosted.org/packages/6d/f4/77f0836c3bda055e35851a22be3bafeb9068c2bb19d011d20631b4d4ad5d/DjTbot-0.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2ec0c6c9395efcf4a3aabb4d9bdd40e714664b814d11b09ff8db90f6835af7f3",
"md5": "8f7f8e84973563dd0863beb7f176eee6",
"sha256": "4af410fa0d9356585a782339b433d56293d54ee916be2bb4b765323504accbea"
},
"downloads": -1,
"filename": "DjTbot-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "8f7f8e84973563dd0863beb7f176eee6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 12180,
"upload_time": "2023-07-29T22:08:03",
"upload_time_iso_8601": "2023-07-29T22:08:03.599597Z",
"url": "https://files.pythonhosted.org/packages/2e/c0/c6c9395efcf4a3aabb4d9bdd40e714664b814d11b09ff8db90f6835af7f3/DjTbot-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-29 22:08:03",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "djtbot"
}