From cb0bbd5685815f8b3b75603b07671c1e0f6fc6ad Mon Sep 17 00:00:00 2001 From: pinibat Date: Tue, 2 May 2023 16:37:18 +0100 Subject: [PATCH 1/2] Adding Pushbullet integration This allows a call to the send_notification function of bot.py so that updates can be sent to whatever device you have recieving pushbullet notifications. For example, a good use case would be for bot developers add a notification hook when we have run out of runes or resources when high alching. The API key is configured via settings and is unique to the bot user. The pushbullet API key (Access token) can be found at https://www.pushbullet.com/#settings/account. If the API key is not configured, bot scripts will continue as normal. --- requirements.txt | 1 + src/OSBC.py | 2 +- src/model/bot.py | 10 ++++++++++ src/view/settings_view.py | 13 +++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9cdc9080..36c2e32b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ pynput==1.7.6 pywinctl==0.0.42 requests==2.28.1 simplejson==3.17.6 +pushbullet-python==1.7.0 \ No newline at end of file diff --git a/src/OSBC.py b/src/OSBC.py index 2e04e833..27dc08c9 100644 --- a/src/OSBC.py +++ b/src/OSBC.py @@ -196,7 +196,7 @@ def __init_settings(self): # ============ Button Handlers ============ def __on_settings_clicked(self): window = customtkinter.CTkToplevel(master=self) - window.geometry("540x287") + window.geometry("540x400") window.title("Settings") view = SettingsView(parent=window) view.pack(side="top", fill="both", expand=True, padx=20, pady=20) diff --git a/src/model/bot.py b/src/model/bot.py index 48190351..be511e01 100644 --- a/src/model/bot.py +++ b/src/model/bot.py @@ -11,6 +11,7 @@ from abc import ABC, abstractmethod from enum import Enum from typing import List, Union +from pushbullet import API import customtkinter import numpy as np @@ -23,6 +24,8 @@ import utilities.imagesearch as imsearch import utilities.ocr as ocr import utilities.random_util as rd +import utilities.settings as settings + from utilities.geometry import Point, Rectangle from utilities.mouse import Mouse from utilities.options_builder import OptionsBuilder @@ -84,6 +87,7 @@ class Bot(ABC): progress: float = 0 status = BotStatus.STOPPED thread: BotThread = None + api = API() @abstractmethod def __init__(self, game_title, bot_title, description, window: Window): @@ -154,6 +158,8 @@ def play(self): return self.reset_progress() self.set_status(BotStatus.RUNNING) + if settings.get("pushbullet_api_key"): + self.api.set_token(settings.get("pushbullet_api_key")) self.thread = BotThread(target=self.main_loop) self.thread.setDaemon(True) self.thread.start() @@ -596,3 +602,7 @@ def toggle_run(self, toggle_on: bool): self.mouse.click() else: self.log_msg("Run is already off.") + + def send_notification(self, title, body): + if settings.get("pushbullet_api_key"): + self.api.send_note(title, body) diff --git a/src/view/settings_view.py b/src/view/settings_view.py index ea50e3d3..13640059 100644 --- a/src/view/settings_view.py +++ b/src/view/settings_view.py @@ -69,6 +69,18 @@ def __init__(self, parent): ) widget_list.append(self.lbl_keybind_note) + # Pushbullet config + self.frame_pushbullet = customtkinter.CTkFrame(master=self) + self.frame_keybinds.columnconfigure(0, weight=0) # lbl label + self.frame_keybinds.columnconfigure(1, weight=1) # text input + self.pushbullet_label = customtkinter.CTkLabel(master=self.frame_pushbullet, text="Pushbullet API Key:") + self.pushbullet_label.grid(row=0, column=0) + self.pushbullet_widget = customtkinter.CTkEntry(master=self.frame_pushbullet, corner_radius=5, placeholder_text="Enter API Key...") + if settings.get("pushbullet_api_key"): + self.pushbullet_widget.insert(0, settings.get("pushbullet_api_key")) + self.pushbullet_widget.grid(row=0, column=1) + widget_list.append(self.frame_pushbullet) + # Grid layout self.num_of_widgets = len(widget_list) for i in range(self.num_of_widgets): @@ -130,6 +142,7 @@ def save(self, window): settings.set("keybind", settings.default_keybind) print("No keybind set, using default keybind.") settings.set("keybind", self.current_keys) + settings.set("pushbullet_api_key", self.pushbullet_widget.get().strip()) print(f"Keybind set to {settings.keybind_to_text(self.current_keys)}") print("Please restart OSBC for changes to take effect.") window.destroy() From c47e4e0e03c3dcdf5b66d22d268a593bc5d3c634 Mon Sep 17 00:00:00 2001 From: pinibat Date: Wed, 3 May 2023 08:43:37 +0100 Subject: [PATCH 2/2] Renaming api variable to pushbullet so it's more obvious what its used for --- src/model/bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/model/bot.py b/src/model/bot.py index be511e01..7ccb412d 100644 --- a/src/model/bot.py +++ b/src/model/bot.py @@ -87,7 +87,7 @@ class Bot(ABC): progress: float = 0 status = BotStatus.STOPPED thread: BotThread = None - api = API() + pushbullet = API() @abstractmethod def __init__(self, game_title, bot_title, description, window: Window): @@ -159,7 +159,7 @@ def play(self): self.reset_progress() self.set_status(BotStatus.RUNNING) if settings.get("pushbullet_api_key"): - self.api.set_token(settings.get("pushbullet_api_key")) + self.pushbullet.set_token(settings.get("pushbullet_api_key")) self.thread = BotThread(target=self.main_loop) self.thread.setDaemon(True) self.thread.start() @@ -605,4 +605,4 @@ def toggle_run(self, toggle_on: bool): def send_notification(self, title, body): if settings.get("pushbullet_api_key"): - self.api.send_note(title, body) + self.pushbullet.send_note(title, body)