diff --git a/node_monitor/bot_telegram.py b/node_monitor/bot_telegram.py index 23609bc9..ef77fd65 100644 --- a/node_monitor/bot_telegram.py +++ b/node_monitor/bot_telegram.py @@ -6,12 +6,12 @@ def __init__(self, telegram_token: str) -> None: self.telegram_token = telegram_token def send_message( - self, channel_id: str, message: str + self, chat_id: str, message: str ) -> None | requests.exceptions.HTTPError: try: request = requests.get( f"https://api.telegram.org/bot{self.telegram_token}" - f"/sendMessage?chat_id={channel_id}&text={message}" + f"/sendMessage?chat_id={chat_id}&text={message}" ) request.raise_for_status() except requests.exceptions.HTTPError as e: diff --git a/node_monitor/node_monitor.py b/node_monitor/node_monitor.py index f9e03989..1c0a271f 100644 --- a/node_monitor/node_monitor.py +++ b/node_monitor/node_monitor.py @@ -38,7 +38,7 @@ def __init__( Attributes: email_bot: An instance of EmailBot slack_bot: An instance of SlackBot - telegram_bot: An instance of Telegram + telegram_bot: An instance of TelegramBot node_provider_db: An instance of NodeProviderDB snapshots: A deque of the last 3 snapshots of the nodes last_update: The timestamp of the last time the nodes were synced @@ -113,12 +113,14 @@ def broadcast_alerts(self) -> None: if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] self.email_bot.send_emails(recipients, subject, msg) - if preferences['notify_slack'] == True and self.slack_bot is not None: - channel_name = channels[node_provider_id]['slack_channel_name'] - self.slack_bot.send_message(channel_name, msg) - if preferences['notify_telegram_channel'] == True and self.telegram_bot is not None: - channel_id = channels[node_provider_id]['telegram_channel_id'] - self.telegram_bot.send_message(channel_id, msg) + if preferences['notify_slack'] == True: + if self.slack_bot is not None: + channel_name = channels[node_provider_id]['slack_channel_name'] + self.slack_bot.send_message(channel_name, msg) + if preferences['notify_telegram_chat'] == True: + if self.telegram_bot is not None: + chat_id = channels[node_provider_id]['telegram_chat_id'] + self.telegram_bot.send_message(chat_id, msg) # - - - - - - - - - - - - - - - - - @@ -147,12 +149,14 @@ def broadcast_status_report(self) -> None: if preferences['notify_email'] == True: recipients = email_recipients[node_provider_id] self.email_bot.send_emails(recipients, subject, msg) - if preferences['notify_slack'] == True and self.slack_bot is not None: - channel_name = channels[node_provider_id]['slack_channel_name'] - self.slack_bot.send_message(channel_name, msg) - if preferences['notify_telegram_channel'] == True and self.telegram_bot is not None: - channel_id = channels[node_provider_id]['telegram_channel_id'] - self.telegram_bot.send_message(channel_id, msg) + if preferences['notify_slack'] == True: + if self.slack_bot is not None: + channel_name = channels[node_provider_id]['slack_channel_name'] + self.slack_bot.send_message(channel_name, msg) + if preferences['notify_telegram_chat'] == True: + if self.telegram_bot is not None: + chat_id = channels[node_provider_id]['telegram_chat_id'] + self.telegram_bot.send_message(chat_id, msg) # - - - - - - - - - - - - - - - - - diff --git a/node_monitor/node_provider_db.py b/node_monitor/node_provider_db.py index fb161c0f..3cc7064f 100644 --- a/node_monitor/node_provider_db.py +++ b/node_monitor/node_provider_db.py @@ -36,13 +36,14 @@ class NodeProviderDB: notify_email BOOLEAN, notify_slack BOOLEAN, notify_telegram_chat BOOLEAN, - notify_telegram_channel BOOLEAN, + notify_telegram_channel BOOLEAN, --Deprecated: column not used node_provider_name TEXT ); """ table_subscribers_cols = [ 'node_provider_id', 'notify_on_status_change', 'notify_email', - 'notify_slack', 'notify_telegram_chat', 'notify_telegram_channel', + 'notify_slack', 'notify_telegram_chat', + 'notify_telegram_channel', # Deprecated: column not used 'node_provider_name'] @@ -65,11 +66,13 @@ class NodeProviderDB: node_provider_id TEXT, slack_channel_name TEXT, telegram_chat_id TEXT, - telegram_channel_id TEXT + telegram_channel_id TEXT --Deprecated: column not used ); """ table_channel_lookup_cols = ['id', 'node_provider_id', 'slack_channel_name', - 'telegram_chat_id', 'telegram_channel_id'] + 'telegram_chat_id', + 'telegram_channel_id' # Deprecated: column not used + ] # TABLE node_label_lookup @@ -172,7 +175,8 @@ def get_public_schema_tables(self) -> List[str]: def _insert_subscriber( self, node_provider_id: Principal, notify_on_status_change: bool, notify_email: bool, notify_slack: bool, notify_telegram_chat: bool, - notify_telegram_channel: bool, node_provider_name: str) -> None: + notify_telegram_channel: bool, # Deprecated: column not used + node_provider_name: str) -> None: """Inserts a subscriber into the subscribers table. Overwrites if subscriber already exists.""" query = """ @@ -182,7 +186,7 @@ def _insert_subscriber( notify_email, notify_slack, notify_telegram_chat, - notify_telegram_channel, + notify_telegram_channel, --Deprecated: colunn not used node_provider_name ) VALUES (%s, %s, %s, %s, %s, %s, %s) ON CONFLICT (node_provider_id) DO UPDATE SET @@ -190,7 +194,7 @@ def _insert_subscriber( notify_email = EXCLUDED.notify_email, notify_slack = EXCLUDED.notify_slack, notify_telegram_chat = EXCLUDED.notify_telegram_chat, - notify_telegram_channel = EXCLUDED.notify_telegram_channel, + notify_telegram_channel = EXCLUDED.notify_telegram_channel, --Deprecated: column not used node_provider_name = EXCLUDED.node_provider_name """ values = ( @@ -199,7 +203,7 @@ def _insert_subscriber( notify_email, notify_slack, notify_telegram_chat, - notify_telegram_channel, + notify_telegram_channel, # Deprecated: column not used node_provider_name ) self.connect() @@ -307,21 +311,24 @@ def get_emails_as_dict(self) -> Dict[Principal, List[str]]: def _insert_channel( self, node_provider_id: Principal, slack_channel_name: str, - telegram_chat_id: str, telegram_channel_id: str) -> None: + telegram_chat_id: str, + telegram_channel_id: str # Deprecated: column not used + ) -> None: """Inserts or updates a record in the channel_lookup table.""" query = """ INSERT INTO channel_lookup ( node_provider_id, slack_channel_name, telegram_chat_id, - telegram_channel_id + telegram_channel_id --Deprecated: column not used ) VALUES (%s, %s, %s, %s) """ values = ( node_provider_id, slack_channel_name, telegram_chat_id, - telegram_channel_id) + telegram_channel_id # Deprecated: column not used + ) self.connect() assert self.conn is not None with self.conn.cursor() as cur: diff --git a/tests/test_bot_telegram.py b/tests/test_bot_telegram.py index 1e91be7b..96216af5 100644 --- a/tests/test_bot_telegram.py +++ b/tests/test_bot_telegram.py @@ -7,15 +7,15 @@ @patch("requests.get") def test_send_message(mock_get): telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) - channel_id = "1234567890" + chat_id = "1234567890" message = "Test message" mock_response = mock_get.return_value mock_response.raise_for_status.return_value = None - telegram_bot.send_message(channel_id, message) + telegram_bot.send_message(chat_id, message) mock_get.assert_called_once_with( - f"https://api.telegram.org/bot{telegram_bot.telegram_token}/sendMessage?chat_id={channel_id}&text={message}" + f"https://api.telegram.org/bot{telegram_bot.telegram_token}/sendMessage?chat_id={chat_id}&text={message}" ) mock_response.raise_for_status.assert_called_once() @@ -24,9 +24,9 @@ def test_send_message(mock_get): @pytest.mark.live_telegram def test_send_live_message(): telegram_bot = TelegramBot(c.TOKEN_TELEGRAM) - channel_id = "-1001925583150" + chat_id = "-1001925583150" message = "Test message" - err = telegram_bot.send_message(channel_id, message) + err = telegram_bot.send_message(chat_id, message) if err is not None: raise err diff --git a/tests/test_node_monitor.py b/tests/test_node_monitor.py index 4c04f986..c137a32f 100644 --- a/tests/test_node_monitor.py +++ b/tests/test_node_monitor.py @@ -29,7 +29,9 @@ 'notify_email': True, 'notify_slack': True, 'notify_telegram_chat': True, - 'notify_telegram_channel': True}} + 'notify_telegram_channel': True # Deprecated: column not used + } + } mock_node_provider_db.get_node_labels_as_dict.return_value = \ {'77fe5-a4oq4-o5pk6-glxt7-ejfpv-tdkrr-24mgs-yuvvz-2tqx6-mowdr-eae': 'dummy-node-label-1', 'clb2i-sz6tk-tlcpr-hgnfv-iybzf-ytorn-dmzkz-m2iw2-lpkqb-l455g-pae': 'dummy-node-label-2'} @@ -42,7 +44,9 @@ 'node_provider_id': 'rbn2y-6vfsb-gv35j-4cyvy-pzbdu-e5aum-jzjg6-5b4n5-vuguf-ycubq-zae', 'slack_channel_name': '#node-monitor', 'telegram_chat_id' : '5734534558', - 'telegram_channel_id': '-1001925583150'} } + 'telegram_channel_id': '-1001925583150' # Deprecated: column not used + } + } # Note that reset_mock() doesn’t clear the return value, side_effect or any # child attributes you have set using normal assignment by default @@ -54,8 +58,8 @@ class TestNodeMonitor: mock_email_bot = Mock(spec=EmailBot) mock_slack_bot = Mock(spec=SlackBot) mock_telegram_bot = Mock(spec=TelegramBot) - nm = NodeMonitor(mock_email_bot, mock_slack_bot, - mock_telegram_bot, mock_node_provider_db) + nm = NodeMonitor(mock_node_provider_db, mock_email_bot, + mock_slack_bot, mock_telegram_bot) nm._resync(cached['control']) nm._resync(cached['control']) nm._resync(cached['control']) @@ -158,9 +162,6 @@ def test_control_only_email_bot(): def test_one_node_bounce(): """Test the case where one node bounces. Should not result in a false positive. - - This also tests that Node Monitor runs - correctly with optional arguments. """ # init mock_email_bot = Mock(spec=EmailBot) diff --git a/tests/test_node_provider_db.py b/tests/test_node_provider_db.py index 3edff53f..c895992f 100644 --- a/tests/test_node_provider_db.py +++ b/tests/test_node_provider_db.py @@ -42,16 +42,19 @@ def test_validate_column_names(): @pytest.mark.db def test_subscribers_crud(): # Create new subscribers / overwrite subscriber 1 + # Note: 6th argument of _insert_subscriber is deprecated node_provider_db._insert_subscriber('test-dummy-principal-1', True, True, False, False, False, 'test-dummy-node-provider-name-1') node_provider_db._insert_subscriber('test-dummy-principal-2', True, True, False, False, False, 'test-dummy-node-provider-name-2') node_provider_db._insert_subscriber('test-dummy-principal-1', True, True, True, True, True, 'test-dummy-node-provider-name-1') # Get and check subscribers + # Note: 6th argument of asssert statement is deprecated subs = node_provider_db.get_subscribers() assert ('test-dummy-principal-1', True, True, True, True, True, 'test-dummy-node-provider-name-1') in subs assert ('test-dummy-principal-2', True, True, False, False, False, 'test-dummy-node-provider-name-2') in subs # Get and check subscribers as dict + # Note: notify_telegram_channel field in assert statement is deprecated subs = node_provider_db.get_subscribers_as_dict() assert subs['test-dummy-principal-1'] == \ {'node_provider_id': 'test-dummy-principal-1', 'notify_on_status_change': True, 'notify_email': True, @@ -65,6 +68,7 @@ def test_subscribers_crud(): node_provider_db._delete_subscriber('test-dummy-principal-2') # Get and check subscribers + # Note: 6th argument of asssert statement is deprecated subs = node_provider_db.get_subscribers() assert ('test-dummy-principal-1', True, True, True, True, True, 'test-dummy-node-provider-name-1') not in subs assert ('test-dummy-principal-2', True, True, False, False, False, 'test-dummy-node-provider-name-2') not in subs @@ -128,6 +132,7 @@ def test_email_lookup_crud(): @pytest.mark.db def test_channel_lookup_crud(): # Insert new channels, including duplicate principals + # Note: last argument of _insert_channel() is deprecated node_provider_db._insert_channel( 'test-dummy-principal-1', 'dummy-slack-channel-1', 'dummy-telegram-chat-1', 'dummy-telegram-channel-1') node_provider_db._insert_channel( @@ -136,14 +141,17 @@ def test_channel_lookup_crud(): 'test-dummy-principal-1', 'dummy-slack-channel-3', 'dummy-telegram-chat-3', 'dummy-telegram-channel-3') # Get the channels, remove surrogate id column + # Note: row[4] is deprecated - telegram channel not used channels = node_provider_db.get_channels() channels = [(row[1], row[2], row[3], row[4]) for row in channels] # Check that the proper channels were inserted correctly + # Note: last argument of assert statement is deprecated assert ('test-dummy-principal-1', 'dummy-slack-channel-1', 'dummy-telegram-chat-1', 'dummy-telegram-channel-1') in channels assert ('test-dummy-principal-2', 'dummy-slack-channel-2', 'dummy-telegram-chat-2', 'dummy-telegram-channel-2') in channels assert ('test-dummy-principal-1', 'dummy-slack-channel-3', 'dummy-telegram-chat-3', 'dummy-telegram-channel-3') in channels + # Note: telegram_channel_id is deprecated channels = node_provider_db.get_channels_as_dict() assert channels['test-dummy-principal-1'] == \ {'node_provider_id': 'test-dummy-principal-1', 'slack_channel_name': 'dummy-slack-channel-3', @@ -157,10 +165,12 @@ def test_channel_lookup_crud(): node_provider_db._delete_channel_lookup('test-dummy-principal-2') # Get the channels, remove surrogate id column + # Note: row[4] is deprecated - telegram channel not used channels = node_provider_db.get_channels() channels = [(row[1], row[2], row[3], row[4]) for row in channels] # Check that the channel lookups were deleted correctly + # Note: last argument of assert statement is deprecated assert ('test-dummy-principal-1', 'dummy-slack-channel-1', 'dummy-telegram-chat-1', 'dummy-telegram-channel-1') not in channels assert ('test-dummy-principal-2', 'dummy-slack-channel-2', 'dummy-telegram-chat-2', 'dummy-telegram-channel-2') not in channels assert ('test-dummy-principal-1', 'dummy-slack-channel-3', 'dummy-telegram-chat-3', 'dummy-telegram-channel-3') not in channels