add environment variable support for configuration

This commit is contained in:
Антон Скалкин 2025-02-18 17:40:36 +04:00
parent 9b3fd192bc
commit 3f7300ef67
3 changed files with 13 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
datetime.json
.env
# ---> Python
# Byte-compiled / optimized / DLL files

17
main.py
View File

@ -1,19 +1,23 @@
import json
import os
from datetime import datetime, timedelta
import pyodbc
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
messageTo = {'sam': '331491581', 'is': '-1001637207513'}
messageTo = os.getenv('MESSAGE_IS')
driverFrom = {'lxc': '/opt/microsoft/msodbcsql18/lib64/libmsodbcsql-18.0.so.1.1', 'windows': '{ODBC Driver 17 for SQL Server}'}
bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
server = 'sis.ustu\SQLEXPRESS'
database = 'DepartmentDatabaseContext'
username = 'tgbot'
password = 'kafISbot'
server = os.getenv('DB_SERVER')
database = os.getenv('DB_DATABASE')
username = os.getenv('DB_USERNAME')
password = os.getenv('DB_PASSWORD')
cnxn = pyodbc.connect(
'DRIVER=' + driverFrom.get('lxc') + ';SERVER=' + server + ';' +
'DATABASE=' + database + ';UID=' + username + ';PWD=' + password + ';Encrypt=no')
@ -39,8 +43,9 @@ def main():
textNews = textNews[:3000] + '...'
params = {'chat_id': messageTo.get('is'),
'text': f'{row[1]}\n{textNews}\nhttp://is.ulstu.ru/News/ShowNews/{row[0]}'}
requests.get('https://api.telegram.org/bot5567223643:AAG6DYNUNq7BNqm7-pI2p-SdvEmAKielViE/sendMessage',
requests.get(f'https://api.telegram.org/bot{bot_token}/sendMessage',
params=params)
date = row[3]
row = cursor.fetchone()

View File

@ -1,3 +1,4 @@
bs4
pyodbc
requests
python-dotenv