-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ gunicorn | |
Flask | ||
sslcrypto | ||
pyTelegramBotAPI | ||
uptime | ||
datetime | ||
tg-logger | ||
tg-logger | ||
pytz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import logging | ||
import os | ||
|
||
import telebot | ||
import tg_logger | ||
from flask import Flask | ||
|
||
# ------------- bot ------------- | ||
bot = telebot.TeleBot(os.environ.get('BOT_TOKEN')) | ||
|
||
# ------------- flask app ------------- | ||
app = Flask(__name__) | ||
|
||
# ------------- logging ------------- | ||
logger = logging.getLogger("tg-bot-template") | ||
|
||
users = [int(os.environ.get("ADMIN_ID"))] | ||
alpha_logger = logging.getLogger() | ||
alpha_logger.setLevel(logging.INFO) | ||
|
||
tg_logger.setup(alpha_logger, token=os.environ.get("LOG_BOT_TOKEN"), users=users) | ||
|
||
app.logger.setLevel(logging.ERROR) | ||
tg_logger.setup(app.logger, token=os.environ.get("LOG_BOT_TOKEN"), users=users) | ||
|
||
telebot.logger.setLevel(logging.ERROR) | ||
tg_logger.setup(telebot.logger, token=os.environ.get("LOG_BOT_TOKEN"), users=users) | ||
|
||
# ------------- webhook ------------- | ||
ADMIN_PASSWORD = os.environ.get('ADMIN_PASSWORD') | ||
WEBHOOK_TOKEN = os.environ.get('WEBHOOK_TOKEN') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import datetime | ||
import time | ||
import traceback | ||
|
||
import pytz | ||
from flask import request | ||
|
||
from setup import * | ||
|
||
# ------------- server boot time ------------- | ||
boot_time = time.time() | ||
boot_date = datetime.datetime.now(tz=pytz.timezone("Europe/Moscow")) | ||
|
||
|
||
# -------------- Exception handler -------------- | ||
@app.errorhandler(Exception) | ||
def handle_exception(e): | ||
logger.error(traceback.format_exc()) | ||
# return "Oops", 500 | ||
|
||
|
||
# -------------- status webpage -------------- | ||
@app.route('/') | ||
def status(): | ||
password = request.args.get("password") | ||
if password != ADMIN_PASSWORD: | ||
logger.info('Status page loaded without password') | ||
return "<h1>Access denied!<h1>", 403 | ||
|
||
return f'<h1>This is telegram bot server, ' \ | ||
f'<a href="https://github.com/otter18/telegram-bot-template">templated</a> by ' \ | ||
f'<a href="https://github.com/otter18">@otter18</a></h1>' \ | ||
f'<p>Server uptime: {datetime.timedelta(seconds=time.time() - boot_time)}</p>' \ | ||
f'<p>Server last boot at {boot_date}' | ||
|
||
|
||
# ------------- webhook ---------------- | ||
@app.route('/' + WEBHOOK_TOKEN, methods=['POST']) | ||
def getMessage(): | ||
temp = request.stream.read().decode("utf-8") | ||
temp = telebot.types.Update.de_json(temp) | ||
logger.debug('New message received. raw: %s', temp) | ||
bot.process_new_updates([temp]) | ||
return "!", 200 | ||
|
||
|
||
@app.route("/set_webhook") | ||
def webhook_on(): | ||
password = request.args.get("password") | ||
if password != ADMIN_PASSWORD: | ||
logger.info('Set_webhook page loaded without password') | ||
return "<h1>Access denied!<h1>", 403 | ||
|
||
bot.remove_webhook() | ||
url = 'https://' + os.environ.get('HOST') + '/' + WEBHOOK_TOKEN | ||
bot.set_webhook(url=url) | ||
logger.info(f'Webhook is ON! Url: %s', url) | ||
return "<h1>WebHook is ON!</h1>", 200 | ||
|
||
|
||
@app.route("/remove_webhook") | ||
def webhook_off(): | ||
password = request.args.get("password") | ||
if password != ADMIN_PASSWORD: | ||
logger.info('Remove_webhook page loaded without password') | ||
return "<h1>Access denied!<h1>", 403 | ||
|
||
bot.remove_webhook() | ||
logger.info('WebHook is OFF!') | ||
return "<h1>WebHook is OFF!</h1>", 200 |