Skip to content
26 changes: 26 additions & 0 deletions boxoffice/messageclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import requests

from baseframe import _

from . import app, rq
from .models import Order


@rq.job('boxoffice')
def send_telegram_message(order_id: int) -> None:
with app.test_request_context():
order = Order.query.get(order_id)
Comment thread
djamg marked this conversation as resolved.
# if order is none block the rest of the code
if order is not None:
message_text = ""
for line_item in order.line_items:
message_text += _("{user} purchased {title}\n").format(
user=order.buyer_fullname, title=line_item.item.title
)
send_text = f'https://api.telegram.org/bot{app.config["TELEGRAM_APIKEY"]}/sendMessage'
params = {
'chat_id': app.config['TELEGRAM_CHATID'],
'message_thread_id': app.config.get['TELEGRAM_THREADID'],
'text': message_text,
}
requests.post(send_text, data=params, timeout=30)
3 changes: 3 additions & 0 deletions boxoffice/views/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
send_order_refund_mail,
send_receipt_mail,
)
from ..messageclient import send_telegram_message
from ..models import (
CURRENCY,
CURRENCY_SYMBOL,
Expand Down Expand Up @@ -438,6 +439,8 @@ def payment(order):
invoice_no=order.invoice_no,
),
)
if app.config.get('TELEGRAM_APIKEY') and app.config.get('TELEGRAM_CHATID'):
send_telegram_message.queue(order_id=order.id)
return api_success(
result={'invoice_id': invoice.id},
doc=_("Payment verified"),
Expand Down
4 changes: 4 additions & 0 deletions instance/settings-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@
#: RQ settings
RQ_REDIS_URL = 'redis://localhost:6379/0'
RQ_SCHEDULER_INTERVAL = 1
#: Telegram settings
TELEGRAM_APIKEY = '' # nosec
TELEGRAM_CHATID = '' # nosec
TELEGRAM_THREADID = ''