epic-games-notifier-telegram/main.py

63 lines
2.4 KiB
Python
Raw Normal View History

2022-12-10 15:09:51 +01:00
from src.commands import command_soon, command_subscribe, command_start, command_freegame #All commands of the bot are in the folder src.commands
from src.events import check_game #All events of the bot are in the folder src.events (ex. check_game = check when Epic Games change the game)
2023-03-06 17:23:21 +01:00
from flask import Flask
2022-12-10 15:09:51 +01:00
from bson.objectid import ObjectId #Library for MongoDB (read ObjectId)
from dotenv import load_dotenv #Library for .env file
from telebot import telebot #Libraries for Telegram bot
import os #Library for .env file (get env variables)
import urllib #Library for MongoDB (read password)
import pymongo #Library for MongoDB
import threading #Library for threading (ex. Check game every 10 seconds)
#Load all variables from .env file
load_dotenv() #Load .env file
2023-03-06 17:23:21 +01:00
app = Flask(__name__)
2022-12-10 15:09:51 +01:00
API_TOKEN = os.getenv('BOT_TOKEN') #Token for Telegram bot
2022-12-11 12:04:41 +01:00
USER_MONGO = os.getenv('USER_MONGODB') #User for MongoDB
2022-12-10 15:09:51 +01:00
PASSWORD_MONGO = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
URL_MONGO = os.getenv('URL_MONGODB') #URL for MongoDB
#Connect to MongoDB
2022-12-11 12:27:22 +01:00
mongo_url = "mongodb+srv://stefano:" + urllib.parse.quote_plus(PASSWORD_MONGO) + URL_MONGO #URL for MongoDB (with password)
2022-12-10 15:09:51 +01:00
client = pymongo.MongoClient(mongo_url) #Connect to MongoDB
2022-12-11 12:27:22 +01:00
database = client["epicgames-notifier"] #Database name
collection_id = database["id-user"] #Collection name (id-user)
collection_game = database["list-game"] #Collection name (list-game)
2022-12-10 15:09:51 +01:00
bot = telebot.TeleBot(API_TOKEN) #Connect to Telegram API
2023-03-06 17:23:21 +01:00
@app.route('/')
def homepage():
return 'Hello'
2022-09-05 15:53:42 +02:00
#Command /start
@bot.message_handler(commands=['start'])
def start(message, bot=bot):
2022-12-10 15:09:51 +01:00
command_start.start_command(message, bot)
2022-09-05 15:53:42 +02:00
#Command /comingsoon
@bot.message_handler(commands=['comingsoon'])
def comingsoon(message, bot=bot):
2022-12-10 15:09:51 +01:00
command_soon.soon_command(message, bot)
2022-09-05 16:59:09 +02:00
2022-09-05 17:18:59 +02:00
#Command /freenow
@bot.message_handler(commands=['freenow'])
def freegame_command(message, bot=bot):
command_freegame.freegame_command(message, bot)
2022-09-05 17:18:59 +02:00
#Command /subscribe
@bot.message_handler(commands=['subscribe'])
def subscribe(message, bot=bot, collection_id=collection_id):
command_subscribe.subscribe_command(message, bot, collection_id)
def event_game(collection_game=collection_game, collection_id=collection_id, bot=bot):
check_game.a(collection_game, collection_id, bot)
2022-12-10 15:09:51 +01:00
#Threading for check game every 5 seconds
t1 = threading.Thread(target=event_game, args=())
t1.start()
2023-03-06 17:23:21 +01:00
if __name__ == '__main__':
app.run(port=8080)
bot.polling()