Эх сурвалжийг харах

Also send department lotery to telegram

JoostSijm 5 жил өмнө
parent
commit
083b2f1661
4 өөрчлөгдсөн 24 нэмэгдсэн , 15 устгасан
  1. 4 0
      app/__init__.py
  2. 1 1
      app/__main__.py
  3. 18 14
      app/app.py
  4. 1 0
      example.env

+ 4 - 0
app/__init__.py

@@ -3,6 +3,7 @@
 import os
 import logging
 
+import telegram
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
 from dotenv import load_dotenv
@@ -13,6 +14,9 @@ from app.models import Base, Player, State, Department, DepartmentStat
 
 load_dotenv()
 
+# Telegram
+TELEGRAM_BOT = telegram.Bot(os.environ['TELEGRAM_KEY'])
+
 # database
 ENGINE = create_engine(os.environ["DATABASE_URI"], client_encoding='utf8')
 SESSION = sessionmaker(bind=ENGINE)

+ 1 - 1
app/__main__.py

@@ -53,7 +53,7 @@ if __name__ == '__main__':
     # jobs
     # jobs.update_department(2788, 6)
     # jobs.send_progress_message(2788, 6, 'nl')
-    # jobs.send_lotery_message(2788, 6, 'nl', 1e9)
+    # jobs.send_lotery_message(2788, 6, 'nl', 32e8)
     # sys.exit()
 
     # Jobs

+ 18 - 14
app/app.py

@@ -4,7 +4,7 @@ import random
 import math
 import re
 
-from app import LOGGER, database, api
+from app import LOGGER, TELEGRAM_BOT, database, api
 
 
 def update_department(state_id, department_type):
@@ -86,17 +86,21 @@ def send_lotery_message(state_id, department_type, language, amount):
         '"%s": candidates "%s", winner "%s" with "%s" points',
         state_id, professor_count, winner_name, amount_of_points
     )
-    msg_winner = "De department loterij is gewonnen door: {:}".format(
-        winner_name
-    )
-    msg_amount = "Met {:} punten heb je $ {:,.0f} gewonnen".format(
-        amount_of_points, amount_of_points * amount
-    ).replace(',', '.')
-    msg_end = "Stuur me een bericht om de prijs te ontvangen."
-    message = '. '.join([
-        msg_winner,
-        msg_amount,
-        msg_end
+    tg_message = '. '.join([
+        "The daily department lotery is won by: {:}".format(winner_name),
+        "With {:} points you won $ {:,.0f}".format(
+            amount_of_points, amount_of_points * amount
+        ).replace(',', '.'),
+        "Send @bergjnl a message to receive your price.",
     ])
-    print(message)
-    api.send_message(language, message)
+    print(tg_message)
+    TELEGRAM_BOT.sendMessage(chat_id='@vn_lottery', text=tg_message)
+    rr_message = '. '.join([
+        "De department loterij is gewonnen door: {:}".format(winner_name),
+        "Met {:} punten heb je $ {:,.0f} gewonnen".format(
+            amount_of_points, amount_of_points * amount
+        ).replace(',', '.'),
+        "Stuur me een bericht om de prijs te ontvangen.",
+    ])
+    print(rr_message)
+    api.send_message(language, rr_message)

+ 1 - 0
example.env

@@ -1,3 +1,4 @@
 AUTHORIZATION=PLACEHOLDER
 DATABASE_URI='postgresql://hvs@localhost/hvs'
 API_URL='http://localhost:5000/api/request/'
+TELEGRAM_KEY=PLACEHOLDER