-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jerimumhs/feature/bot_class
Feature/bot class
- Loading branch information
Showing
21 changed files
with
304 additions
and
220 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,4 @@ | ||
export BOT_TOKEN=meu_token_123 | ||
export BOT_PROD_TOKEN=meu_token_123 | ||
export MODE=cmd | ||
export SERVER_URL=https://jerimumhsbot.herokuapp.com |
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 |
---|---|---|
|
@@ -2,3 +2,8 @@ config.json | |
.vscode | ||
__pycache__ | ||
*.pyc | ||
|
||
.env | ||
|
||
env/ | ||
.idea/ |
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,2 @@ | ||
run: | ||
python run.py |
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 @@ | ||
web: python run.py |
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,13 @@ | ||
from core import BotTelegramCore | ||
from mixins import (BaseCommandsBotMixin, CallbackBotMixin, ErrorBotMixin, | ||
MessageBotMixin, StickerBotMixin) | ||
|
||
|
||
class JerimumBot(BaseCommandsBotMixin, CallbackBotMixin, ErrorBotMixin, | ||
MessageBotMixin, StickerBotMixin): | ||
"""Bot Controller""" | ||
|
||
def config_handlers(self): | ||
for BaseClass in self.__class__.__bases__: | ||
assert issubclass(BaseClass, BotTelegramCore) | ||
BaseClass.config_handlers(self) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
from core.auth import adm_verify | ||
from core.telegram import BotTelegramCore |
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,3 @@ | ||
|
||
def adm_verify(update): | ||
return update.message.chat.get_member(update.message.from_user.id).status in ('creator', 'administrator') |
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,46 @@ | ||
from abc import ABC, abstractmethod | ||
import logging | ||
|
||
from telegram.ext import Updater | ||
|
||
|
||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
level=logging.INFO) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BotTelegramCore(ABC): | ||
def __init__(self, token, port, server_url): | ||
logging.info('Inicializando o bot...') | ||
self.token = token | ||
self.port = port | ||
self.server_url = server_url | ||
|
||
self.updater = Updater(self.token) | ||
self.config_handlers() | ||
|
||
@abstractmethod | ||
def config_handlers(self): | ||
raise NotImplementedError('Cannot call config_handler from BotCore') | ||
|
||
def run_web(self): | ||
"""Start the bot as a webhook server""" | ||
|
||
self.updater.start_webhook( | ||
listen="0.0.0.0", | ||
port=self.port, | ||
url_path=self.token | ||
) | ||
|
||
self.updater.bot.set_webhook(f"{self.server_url}/{self.token}") | ||
|
||
logging.info('Bot está rodando como um webserver!') | ||
self.updater.idle() | ||
|
||
def run_cmd(self): | ||
"""Start the bot as a python script loop""" | ||
self.updater.start_polling() | ||
|
||
logging.info('Bot está rodando como um script python!') | ||
self.updater.idle() |
File renamed without changes
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
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,5 @@ | ||
from mixins.base_commands import BaseCommandsBotMixin | ||
from mixins.callback import CallbackBotMixin | ||
from mixins.error import ErrorBotMixin | ||
from mixins.message import MessageBotMixin | ||
from mixins.sticker import StickerBotMixin |
Oops, something went wrong.