-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging, UrlTask and GenericTask
- Loading branch information
Showing
7 changed files
with
132 additions
and
17 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
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,46 @@ | ||
from abc import abstractmethod | ||
from typing import List | ||
|
||
import telegram | ||
from telegram.ext import JobQueue | ||
|
||
from telegramtaskbot.Tasks.Task import Task | ||
|
||
|
||
class GenericTask(Task): | ||
job_actual_value: str | ||
generic = True | ||
show_subscribe_buttons = False | ||
|
||
def __init__(self, job_queue: JobQueue = None): | ||
super().__init__() | ||
self.job_actual_value = 'actual_' + self.job_name | ||
|
||
@abstractmethod | ||
def callback(self, context: telegram.ext.CallbackContext): | ||
pass | ||
|
||
def start(self, jobs: List[telegram.ext.Job], update: telegram.Update, context: telegram.ext.CallbackContext): | ||
self.handle_start(context, update.callback_query.message.chat_id) | ||
|
||
def start_command(self, update: telegram.Update, context: telegram.ext.CallbackContext): | ||
self.handle_start(context, update.message.chat_id) | ||
|
||
def handle_start(self, context: telegram.ext.CallbackContext, chat_id: str): | ||
context.bot.send_message(chat_id=chat_id, | ||
text=f'Thank you for subscribing') | ||
self.save_user(chat_id) | ||
self.logger.debug(f'User {chat_id} subscribed') | ||
|
||
def stop(self, jobs: List[telegram.ext.Job], update: telegram.Update, context: telegram.ext.CallbackContext): | ||
self.handle_stop(context, update.callback_query.message.chat_id) | ||
|
||
def stop_command(self, update: telegram.Update, context: telegram.ext.CallbackContext): | ||
self.handle_stop(context, update.message.chat_id) | ||
|
||
def handle_stop(self, context: telegram.ext.CallbackContext, chat_id: str): | ||
users = self.load_users() | ||
users.remove(chat_id) | ||
self.save_to_json(users) | ||
self.logger.debug(f'User {chat_id} unsubscribed') | ||
context.bot.send_message(chat_id=chat_id, text=f'You succesfully unsubscribed') |
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,58 @@ | ||
import time as timer | ||
from abc import abstractmethod | ||
|
||
import requests | ||
import telegram | ||
from requests import Response | ||
from telegram import InlineKeyboardButton | ||
|
||
from telegramtaskbot.Tasks.GenericTask import GenericTask | ||
|
||
|
||
class UrlTask(GenericTask): | ||
disable_notifications = True | ||
url: str | ||
|
||
def callback(self, context: telegram.ext.CallbackContext): | ||
self.logger.info(f'Run {self.job_name}') | ||
users = self.load_users() | ||
response_message = self.get_data() | ||
self.logger.info(f'Notifying {len(users)} users for {self.job_name}') | ||
for user in users: | ||
context.bot.send_message(chat_id=user, text=response_message, | ||
disable_notification=self.disable_notifications) | ||
|
||
def get_actual_value(self, joblist: [], update: telegram.Update, context: telegram.ext.CallbackContext): | ||
self.logger.debug(f'Get actual value from {self.job_name} for {update.callback_query.message.chat_id}') | ||
data: str = self.get_data() | ||
context.bot.send_message(chat_id=update.callback_query.message.chat_id, text=data) | ||
self.logger.debug( | ||
f'Send message to {update.callback_query.message.chat_id} with content: \"{" ".join(data.splitlines())}\"') | ||
|
||
def get_data(self): | ||
return self.handle_response(self.get_response()) | ||
|
||
@abstractmethod | ||
def handle_response(self, response: Response): | ||
pass | ||
|
||
def get_response(self): | ||
count = 0 | ||
response = requests.get(self.url) | ||
if response.status_code != 200: | ||
while response.status_code != 200: | ||
timer.sleep(2) | ||
resp = requests.get(self.url) | ||
count += 1 | ||
response = resp | ||
self.logger.debug(f'{self.job_name} tried for {count} times') | ||
return response | ||
|
||
def get_inline_keyboard(self): | ||
buttons = [ | ||
InlineKeyboardButton(f"Get actual Value for {self.job_name}", callback_data=self.job_actual_value), | ||
] | ||
if self.show_subscribe_buttons: | ||
buttons.append(InlineKeyboardButton(f"Subscribe for {self.job_name}", callback_data=self.job_start_name)) | ||
buttons.append(InlineKeyboardButton(f"Unsubscribe for {self.job_name}", callback_data=self.job_stop_name)) | ||
return buttons |
Empty file.
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
from .Task import Task | ||
import logging | ||
|
||
from .Bot import TelegramTaskBot | ||
from .Tasks.GenericTask import GenericTask | ||
from .Tasks.Task import Task | ||
from .Tasks.UrlTask import UrlTask | ||
|
||
logging.getLogger(__name__).addHandler(logging.NullHandler()) |