87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
import locale
|
||
import logging
|
||
|
||
from flask import Flask, request
|
||
from flask_cors import CORS, cross_origin
|
||
from flask_executor import Executor
|
||
from flask_wtf import FlaskForm, RecaptchaField
|
||
import telegram_send
|
||
from wtforms import StringField
|
||
from wtforms.validators import DataRequired
|
||
|
||
from environs import Env
|
||
|
||
env = Env()
|
||
env.read_env()
|
||
|
||
# Set locale to UTF-8 as server's one is ANSI - this breaks cyrillic logging
|
||
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
||
|
||
logging.basicConfig(filename=f'{env.str("DEPLOY_PATH")}/log.log',
|
||
filemode='a',
|
||
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
|
||
datefmt='%H:%M:%S',
|
||
level=logging.INFO)
|
||
|
||
logger = logging.getLogger()
|
||
|
||
app = Flask(__name__)
|
||
|
||
app.secret_key = env.str("FLASK_APIKEY")
|
||
app.config["APPLICATION_ROOT"] = "/api"
|
||
app.config["WTF_CSRF_ENABLED"] = False
|
||
|
||
app.config["RECAPTCHA_PUBLIC_KEY"] = env.str("RECAPTCHA_PUBLIC_KEY")
|
||
app.config["RECAPTCHA_PRIVATE_KEY"] = env.str("RECAPTCHA_PRIVATE_KEY")
|
||
|
||
app.config['EXECUTOR_TYPE'] = 'process'
|
||
app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
|
||
|
||
executor = Executor(app)
|
||
|
||
|
||
def send_telegram(message, silent=False):
|
||
telegram_send.send(messages=[message], conf=f'{env.str("DEPLOY_PATH")}/telegram.conf', silent=silent)
|
||
|
||
|
||
class ContactForm(FlaskForm):
|
||
name = StringField('Username', validators=[DataRequired()])
|
||
contacts = StringField('Contacts', validators=[DataRequired()])
|
||
message = StringField('Message')
|
||
recaptcha = RecaptchaField()
|
||
|
||
|
||
@app.route('/contact/', methods=['POST'])
|
||
def contact():
|
||
form = ContactForm(request.form)
|
||
|
||
msg_template = """
|
||
Обращение через форму обратной связи с сайта alex-sharoff.ru:
|
||
|
||
Имя: {name}
|
||
Контакты: {contacts}
|
||
-------
|
||
{message}
|
||
"""
|
||
|
||
if form.validate_on_submit():
|
||
text = msg_template.format(
|
||
name=form.name.data,
|
||
contacts=form.contacts.data,
|
||
message=form.message.data
|
||
)
|
||
logger.info(f'{form.name.data} - {form.contacts.data}: [{form.message.data}]')
|
||
executor.submit(send_telegram, text)
|
||
return '', 200
|
||
|
||
return '', 400
|
||
|
||
|
||
@app.route('/')
|
||
def hello_world():
|
||
return 'Hello, World!'
|
||
|
||
|
||
if __name__ == '__main__':
|
||
app.run()
|