Repository: tough-dev-school/education-backend

Issue:
Переписать интеграцию с телеграмом
Сейчас используем какой-то [мутный сервис](https://github.com/f213/education-backend/blob/master/src/app/integrations/tg.py#L4) для отправки сообщения в телегу. Так вышло потому, что когда мы запускались, API телеграма было раскомнадзорено.

Надо отказаться от использования этого сервиса. Заодно, сделать возможность указывать отдельные каналы уведомлений о новых заказах для разных курсов.


Discussion:

Resolving patch:
diff --git a/src/app/integrations/tg.py b/src/app/integrations/tg.py
--- a/src/app/integrations/tg.py
+++ b/src/app/integrations/tg.py
@@ -1,10 +1,18 @@
 import requests
+from django.conf import settings
 
 
-def send_happiness_message(text):
-    response = requests.post('https://timepad.f213.in/msg/', json={
+def send_message(channel, text):
+    url = f'https://api.telegram.org/bot{settings.BOT_TOKEN}/sendMessage'
+    response = requests.post(url, data={
+        'chat_id': channel,
         'text': text,
+        'parse_mode': 'markdown',
+        'disable_web_page_preview': True,
     })
 
-    assert response.status_code == 200, 'TG proxy should return 200'
-    assert response.json()['ok'] is True, 'TG proxy should say msg is ok'
+    assert response.status_code == 200, 'TG should return 200'
+
+
+def send_happiness_message(text):
+    send_message(settings.HAPPINESS_MESSAGES_CHAT_ID, text)
diff --git a/src/app/settings.py b/src/app/settings.py
--- a/src/app/settings.py
+++ b/src/app/settings.py
@@ -232,7 +232,8 @@
 
 TINKOFF_CREDIT_DEMO_MODE = env('TINKOFF_CREDIT_DEMO_MODE', default=DEBUG)
 
-SEND_HAPPINESS_MESSAGES = env('SEND_HAPPINESS_MESSAGES', cast=bool, default=False)
+BOT_TOKEN = env('BOT_TOKEN', cast=str, default=None)
+HAPPINESS_MESSAGES_CHAT_ID = env('HAPPINESS_MESSAGES_CHAT_ID', cast=str, default=None)
 
 DRF_RECAPTCHA_SECRET_KEY = env('RECAPTCHA_SECRET_KEY', cast=str, default='')
 DRF_RECAPTCHA_TESTING = DRF_RECAPTCHA_TESTING_PASS = not env('RECAPTCHA_ENABLED', cast=bool, default=True)
diff --git a/src/orders/services/order_shipper.py b/src/orders/services/order_shipper.py
--- a/src/orders/services/order_shipper.py
+++ b/src/orders/services/order_shipper.py
@@ -36,7 +36,7 @@ def mark_order_as_shipped(self):
         self.order.save()
 
     def send_happiness_message(self):
-        if not settings.SEND_HAPPINESS_MESSAGES:
+        if not settings.HAPPINESS_MESSAGES_CHAT_ID:
             return
 
         send_happiness_message.delay(text='💰+{sum} ₽, {user}, {reason}'.format(


Test patch:
diff --git a/src/orders/tests/order_shipping/tests_sending_happiness_messages.py b/src/orders/tests/order_shipping/tests_sending_happiness_messages.py
--- a/src/orders/tests/order_shipping/tests_sending_happiness_messages.py
+++ b/src/orders/tests/order_shipping/tests_sending_happiness_messages.py
@@ -5,7 +5,7 @@
 
 @pytest.fixture(autouse=True)
 def _enable_happiness_messages(settings):
-    settings.SEND_HAPPINESS_MESSAGES = True
+    settings.HAPPINESS_MESSAGES_CHAT_ID = 'aaa100500'
 
 
 @pytest.fixture
@@ -27,7 +27,7 @@ def test_gift(tg_message, order, another_user):
 
 
 def test_not_sending_if_disabled(settings, tg_message, order):
-    settings.SEND_HAPPINESS_MESSAGES = False
+    settings.HAPPINESS_MESSAGES_CHAT_ID = None
 
     order.set_paid()
 
diff --git a/src/orders/tests/test_shipping_unshipped_orders.py b/src/orders/tests/test_shipping_unshipped_orders.py
--- a/src/orders/tests/test_shipping_unshipped_orders.py
+++ b/src/orders/tests/test_shipping_unshipped_orders.py
@@ -72,7 +72,7 @@ def test_not_shipping_orders_that_should_not_be_shipped(order, ship, change_orde
 
 
 def test_orders_are_shipped_silently(order, send_happiness_message, settings):
-    settings.SEND_HAPPINESS_MESSAGES = True
+    settings.HAPPINESS_MESSAGES_CHAT_ID = 'aaa100500'
 
     tasks.ship_unshipped_orders()