-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/telegram bot #455
base: develop
Are you sure you want to change the base?
Feature/telegram bot #455
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||||||||
import requests | ||||||||||||
import time | ||||||||||||
from collections import defaultdict | ||||||||||||
from django.utils.deprecation import MiddlewareMixin | ||||||||||||
from django.conf import settings | ||||||||||||
|
||||||||||||
API_TOKEN = settings.TELEGRAM_API_TOKEN | ||||||||||||
CHANNEL_ID = settings.TELEGRAM_CHANNEL_ID | ||||||||||||
MIN_INTERVAL = settings.TELEGRAM_MIN_LOG_INTERVAL | ||||||||||||
|
||||||||||||
|
||||||||||||
BASE_URL = f"https://api.telegram.org/bot{API_TOKEN}" | ||||||||||||
|
||||||||||||
|
||||||||||||
def send_telegram_log(text): | ||||||||||||
url = f"{BASE_URL}/sendMessage" | ||||||||||||
payload = { | ||||||||||||
"chat_id": CHANNEL_ID, | ||||||||||||
"text": text, | ||||||||||||
"parse_mode": "MarkdownV2", | ||||||||||||
"disable_web_page_preview": True, | ||||||||||||
} | ||||||||||||
try: | ||||||||||||
response = requests.post(url, data=payload) | ||||||||||||
return {"ok":True} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Consider logging the exception. In the
Suggested change
|
||||||||||||
except Exception as e: | ||||||||||||
return {"ok": False} | ||||||||||||
|
||||||||||||
|
||||||||||||
log_cache = defaultdict(int) | ||||||||||||
|
||||||||||||
|
||||||||||||
|
||||||||||||
class LogMiddleware(MiddlewareMixin): | ||||||||||||
def log_message(self, message): | ||||||||||||
log_hash = hash(message) | ||||||||||||
current_time = time.time() | ||||||||||||
|
||||||||||||
# Check if the log has been sent before | ||||||||||||
if current_time - log_cache[log_hash] > MIN_INTERVAL: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): Potential race condition with log_cache. Accessing and modifying |
||||||||||||
log_cache[log_hash] = current_time | ||||||||||||
return self.send_to_telegram(message) | ||||||||||||
|
||||||||||||
return {"ok": False} | ||||||||||||
|
||||||||||||
def send_to_telegram(self, message): | ||||||||||||
return send_telegram_log(message) | ||||||||||||
|
||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import time | ||
from unittest.mock import PropertyMock, patch | ||
|
||
from django.contrib.auth.models import User | ||
|
@@ -6,6 +7,12 @@ | |
from authentication.models import GitcoinPassportConnection, UserProfile, Wallet | ||
from core.models import Chain, NetworkTypes, WalletAccount | ||
|
||
from django.test import TestCase | ||
from core.telegram import LogMiddleware | ||
from django.conf import settings | ||
|
||
TELEGRAM_MIN_LOG_INTERVAL = settings.TELEGRAM_MIN_LOG_INTERVAL | ||
|
||
from .constraints import ( | ||
Attest, | ||
BeAttestedBy, | ||
|
@@ -363,3 +370,28 @@ def test_gitcoin_passport_connection_constraint_fail(self): | |
constraint = HasGitcoinPassportProfile(self.not_connected_user_profile) | ||
|
||
self.assertEqual(constraint.is_observed(), False) | ||
|
||
|
||
|
||
|
||
|
||
class LogMiddlewareTests(TestCase): | ||
def setUp(self): | ||
self.middleware = LogMiddleware(get_response=lambda request: None) | ||
|
||
|
||
def test_log_message_sent_to_telegram(self): | ||
|
||
res = self.middleware.log_message("Test log message") | ||
self.assertEqual(res['ok'], True) | ||
res = self.middleware.log_message("Test log message") | ||
self.assertEqual(res['ok'], False) | ||
# delay | ||
time.sleep(TELEGRAM_MIN_LOG_INTERVAL) | ||
res = self.middleware.log_message("Test log message") | ||
self.assertEqual(res['ok'], True) | ||
res = self.middleware.log_message("Test log message") | ||
self.assertEqual(res['ok'], False) | ||
Comment on lines
+385
to
+394
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (code-quality): Extract duplicate code into method ( |
||
res = self.middleware.log_message("Test log message2") | ||
self.assertEqual(res['ok'], True) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Environment variables should have default values.
Consider providing default values for
TELEGRAM_CHANNEL_ID
andTELEGRAM_API_TOKEN
to avoid potentialNoneType
errors if the environment variables are not set.