Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send order purchase messages to telegram #473

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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)
djamg marked this conversation as resolved.
Show resolved Hide 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 = ''