Skip to content

Commit

Permalink
replace deprecated channel_id with chat_id
Browse files Browse the repository at this point in the history
  • Loading branch information
louisevelayo committed Oct 16, 2023
1 parent cca6a84 commit 7fbfb8a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 38 deletions.
4 changes: 2 additions & 2 deletions node_monitor/bot_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 17 additions & 13 deletions node_monitor/node_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
# - - - - - - - - - - - - - - - - -


Expand Down Expand Up @@ -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)
# - - - - - - - - - - - - - - - - -


Expand Down
29 changes: 18 additions & 11 deletions node_monitor/node_provider_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']


Expand All @@ -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
Expand Down Expand Up @@ -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 = """
Expand All @@ -182,15 +186,15 @@ 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
notify_on_status_change = EXCLUDED.notify_on_status_change,
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 = (
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_bot_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
15 changes: 8 additions & 7 deletions tests/test_node_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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
Expand All @@ -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'])
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_node_provider_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 7fbfb8a

Please sign in to comment.