Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
web: python run.py
web: flask run -h 0.0.0.0 -p $PORT
worker: python bot.py
6 changes: 4 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

@app.route('/')
def hello():
JerimumBot.send_message(chat_id='-281314498', text='Essa é uma mensagem de teste')
JerimumBot.send_message(
chat_id='-281314498',
text='Essa é uma mensagem de teste'
)
return 'Hello, World!'

25 changes: 25 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import logging

from decouple import config

from core import BotTelegramCore
from commands import handlers


logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)

logger = logging.getLogger(__name__)


class JerimumBot(BotTelegramCore):
"""Bot Controller"""

def config_handlers(self):
for config_handler in handlers:
config_handler(self)

@classmethod
def instance(cls):
if super().instance() is None:
return cls(token=config('BOT_TOKEN', default='??'))
return super().instance()

@classmethod
def run(cls):
super(cls, cls.instance()).run()


if __name__ == '__main__':
JerimumBot.run()
19 changes: 12 additions & 7 deletions core/telegram.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from time import sleep
from abc import ABC, abstractmethod
import logging

Expand All @@ -25,19 +24,21 @@ def __init__(self, token):
self.token = token

self.__updater = Updater(self.token)
self.__running = False
self.config_handlers()

@classmethod
def send_message(cls, chat_id, text, parse_mode=None):
instance = cls.instance()
assert isinstance(instance, BotTelegramCore)
instance.__updater.bot.send_message(chat_id=chat_id, text=text, parse_mode=parse_mode)
instance.__updater.bot.send_message(
chat_id=chat_id,
text=text,
parse_mode=parse_mode
)

@classmethod
def instance(cls):
while cls.__instance is None:
logging.info('Esperando bot ser inicializado...')
sleep(1)
return cls.__instance

@abstractmethod
Expand All @@ -54,6 +55,10 @@ def add_error_handler(self, handler):

def run(self):
"""Start the bot as a python script loop"""
self.__updater.start_polling()
if not self.__running:
self.__updater.start_polling()

logging.info('Bot está rodando como um script python!')
logging.info('Bot está rodando como um script python!')
self.__running = True
else:
logging.info('Bot já está rodando!')
5 changes: 1 addition & 4 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import logging

from decouple import config

from app import app
from bot import JerimumBot


if __name__ == '__main__':
bot = JerimumBot(token=config('BOT_TOKEN', default='??'))
JerimumBot.run()

bot.run()
app.run(host='0.0.0.0', port=config('PORT', default=5000, cast=int))