From 7d2ed60a78fc70580baf4356b9be84167912619f Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Wed, 22 Nov 2023 11:02:11 +0530 Subject: [PATCH 1/9] Send order purchase message to telegram --- boxoffice/messageclient.py | 13 +++++++++++++ boxoffice/views/order.py | 2 ++ instance/settings-sample.py | 2 ++ 3 files changed, 17 insertions(+) create mode 100644 boxoffice/messageclient.py diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py new file mode 100644 index 000000000..036ceedd3 --- /dev/null +++ b/boxoffice/messageclient.py @@ -0,0 +1,13 @@ +import requests +from baseframe import _ + +from . import app, rq + + +@rq.job('boxoffice') +def send_telegram_message(order): + with app.test_request_context(): + message_text = f'{order.buyer_fullname} purchased {order.line_item.item.title}' + send_text = f'https://api.telegram.org/bot{app.config["TELEGRAM_BOT_TOKEN"]}/sendMessage' + params = {'chat_id': app.config["TELEGRAM_CHAT_ID"], 'text': message_text} + requests.post(send_text, data=params, timeout=30) diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index 49f653827..fda7f7910 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -24,6 +24,7 @@ send_order_refund_mail, send_receipt_mail, ) +from ..messageclient import send_telegram_message from ..models import ( CURRENCY, CURRENCY_SYMBOL, @@ -438,6 +439,7 @@ def payment(order): invoice_no=order.invoice_no, ), ) + send_telegram_message.queue(order=order) return api_success( result={'invoice_id': invoice.id}, doc=_("Payment verified"), diff --git a/instance/settings-sample.py b/instance/settings-sample.py index 6e069e02b..0dbcefb8f 100644 --- a/instance/settings-sample.py +++ b/instance/settings-sample.py @@ -40,3 +40,5 @@ #: RQ settings RQ_REDIS_URL = 'redis://localhost:6379/0' RQ_SCHEDULER_INTERVAL = 1 +TELEGRAM_BOT_TOKEN = '' +TELEGRAM_CHAT_ID = '' From 49965ef9a1dec8e7b9b074c94e852159a65fbc49 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Nov 2023 05:43:18 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boxoffice/messageclient.py | 1 - 1 file changed, 1 deletion(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 036ceedd3..73c71ee75 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -1,5 +1,4 @@ import requests -from baseframe import _ from . import app, rq From cdc59399da92a7da24f72c2304acaea2993ef9b5 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Tue, 26 Dec 2023 12:57:21 +0530 Subject: [PATCH 3/9] Added gettext and telegram thread id --- boxoffice/messageclient.py | 12 ++++++++---- boxoffice/views/order.py | 6 +++++- instance/settings-sample.py | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 73c71ee75..371c83d3e 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -1,12 +1,16 @@ import requests from . import app, rq - +from baseframe import _ @rq.job('boxoffice') -def send_telegram_message(order): +def send_telegram_message(buyer_fullname, line_item_title): with app.test_request_context(): - message_text = f'{order.buyer_fullname} purchased {order.line_item.item.title}' + message_text = _(f'{buyer_fullname} purchased {line_item_title}') send_text = f'https://api.telegram.org/bot{app.config["TELEGRAM_BOT_TOKEN"]}/sendMessage' - params = {'chat_id': app.config["TELEGRAM_CHAT_ID"], 'text': message_text} + params = { + 'chat_id': app.config['TELEGRAM_CHAT_ID'], + 'message_thread_id': app.config['TELEGRAM_MESSAGE_THREAD_ID'], + 'text': message_text, + } requests.post(send_text, data=params, timeout=30) diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index fda7f7910..aef379b57 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -439,7 +439,11 @@ def payment(order): invoice_no=order.invoice_no, ), ) - send_telegram_message.queue(order=order) + if app.config['TELEGRAM_BOT_TOKEN']: + send_telegram_message.queue( + buyer_fullname=order.buyer_fullname, + line_item_title=order.line_item.item.title, + ) return api_success( result={'invoice_id': invoice.id}, doc=_("Payment verified"), diff --git a/instance/settings-sample.py b/instance/settings-sample.py index 0dbcefb8f..0593ebeb4 100644 --- a/instance/settings-sample.py +++ b/instance/settings-sample.py @@ -42,3 +42,4 @@ RQ_SCHEDULER_INTERVAL = 1 TELEGRAM_BOT_TOKEN = '' TELEGRAM_CHAT_ID = '' +TELEGRAM_MESSAGE_THREAD_ID = '' From f887e34ca848f3e4ee3b98d9075f34f47152ba8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Dec 2023 07:27:59 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boxoffice/messageclient.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 371c83d3e..83fb974f4 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -1,8 +1,10 @@ import requests -from . import app, rq from baseframe import _ +from . import app, rq + + @rq.job('boxoffice') def send_telegram_message(buyer_fullname, line_item_title): with app.test_request_context(): From ccbe03482be7c1c920db1c2c701a42e7a20b6a32 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Tue, 26 Dec 2023 13:31:48 +0530 Subject: [PATCH 5/9] Fixed gettext --- boxoffice/messageclient.py | 13 +++++++++---- boxoffice/views/order.py | 2 +- instance/settings-sample.py | 7 ++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 371c83d3e..c1da402e2 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -3,14 +3,19 @@ from . import app, rq from baseframe import _ + @rq.job('boxoffice') def send_telegram_message(buyer_fullname, line_item_title): with app.test_request_context(): - message_text = _(f'{buyer_fullname} purchased {line_item_title}') - send_text = f'https://api.telegram.org/bot{app.config["TELEGRAM_BOT_TOKEN"]}/sendMessage' + message_text = _("{user} purchased {title}").format( + user=buyer_fullname, title=line_item_title + ) + send_text = ( + f'https://api.telegram.org/bot{app.config["TELEGRAM_APIKEY"]}/sendMessage' + ) params = { - 'chat_id': app.config['TELEGRAM_CHAT_ID'], - 'message_thread_id': app.config['TELEGRAM_MESSAGE_THREAD_ID'], + '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) diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index aef379b57..77b433606 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -439,7 +439,7 @@ def payment(order): invoice_no=order.invoice_no, ), ) - if app.config['TELEGRAM_BOT_TOKEN']: + if app.config['TELEGRAM_APIKEY'] and app.config['TELEGRAM_CHATID']: send_telegram_message.queue( buyer_fullname=order.buyer_fullname, line_item_title=order.line_item.item.title, diff --git a/instance/settings-sample.py b/instance/settings-sample.py index 0593ebeb4..37bd09eda 100644 --- a/instance/settings-sample.py +++ b/instance/settings-sample.py @@ -40,6 +40,7 @@ #: RQ settings RQ_REDIS_URL = 'redis://localhost:6379/0' RQ_SCHEDULER_INTERVAL = 1 -TELEGRAM_BOT_TOKEN = '' -TELEGRAM_CHAT_ID = '' -TELEGRAM_MESSAGE_THREAD_ID = '' +#: Telegram settings +TELEGRAM_APIKEY = '' # nosec +TELEGRAM_CHATID = '' # nosec +TELEGRAM_THREADID = '' From 8d6c004c9c650fa09ed47d78aac71c90030e50f7 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Tue, 26 Dec 2023 16:46:08 +0530 Subject: [PATCH 6/9] Fixed send_telegram_message --- boxoffice/messageclient.py | 7 +++++-- boxoffice/views/order.py | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 1f3201b3a..e9862b3c8 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -4,12 +4,15 @@ from . import app, rq +from .models import Order + @rq.job('boxoffice') -def send_telegram_message(buyer_fullname, line_item_title): +def send_telegram_message(order_id): with app.test_request_context(): + order = Order.query.get(order_id) message_text = _("{user} purchased {title}").format( - user=buyer_fullname, title=line_item_title + user=order.buyer_fullname, title=order.line_item.item.title ) send_text = ( f'https://api.telegram.org/bot{app.config["TELEGRAM_APIKEY"]}/sendMessage' diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index 77b433606..0f20a9f82 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -441,6 +441,7 @@ def payment(order): ) if app.config['TELEGRAM_APIKEY'] and app.config['TELEGRAM_CHATID']: send_telegram_message.queue( + order.id buyer_fullname=order.buyer_fullname, line_item_title=order.line_item.item.title, ) From ef7b4c0e77ab630a890b0640a77b55949e46f627 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:16:30 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boxoffice/messageclient.py | 1 - 1 file changed, 1 deletion(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index e9862b3c8..22cd915ea 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -3,7 +3,6 @@ from baseframe import _ from . import app, rq - from .models import Order From 7a7032aeaa9ae406883d2398e6131ea9b325e271 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Wed, 27 Dec 2023 11:14:59 +0530 Subject: [PATCH 8/9] Fixed line items --- boxoffice/messageclient.py | 8 +++++--- boxoffice/views/order.py | 6 +----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index 22cd915ea..ff2e4be8e 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -10,9 +10,11 @@ def send_telegram_message(order_id): with app.test_request_context(): order = Order.query.get(order_id) - message_text = _("{user} purchased {title}").format( - user=order.buyer_fullname, title=order.line_item.item.title - ) + 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' ) diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index 0f20a9f82..0aaccad5f 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -440,11 +440,7 @@ def payment(order): ), ) if app.config['TELEGRAM_APIKEY'] and app.config['TELEGRAM_CHATID']: - send_telegram_message.queue( - order.id - buyer_fullname=order.buyer_fullname, - line_item_title=order.line_item.item.title, - ) + send_telegram_message.queue(order_id=order.id) return api_success( result={'invoice_id': invoice.id}, doc=_("Payment verified"), From 329fc0d15967f4baecf3fe78d1771084f3f9d113 Mon Sep 17 00:00:00 2001 From: Amogh M Aradhya Date: Thu, 28 Dec 2023 14:50:14 +0530 Subject: [PATCH 9/9] Fixed telegram notifications --- boxoffice/messageclient.py | 30 +++++++++++++++--------------- boxoffice/views/order.py | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/boxoffice/messageclient.py b/boxoffice/messageclient.py index ff2e4be8e..29f3b7695 100644 --- a/boxoffice/messageclient.py +++ b/boxoffice/messageclient.py @@ -7,20 +7,20 @@ @rq.job('boxoffice') -def send_telegram_message(order_id): +def send_telegram_message(order_id: int) -> None: with app.test_request_context(): order = Order.query.get(order_id) - 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) + # 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) diff --git a/boxoffice/views/order.py b/boxoffice/views/order.py index 0aaccad5f..358d85945 100644 --- a/boxoffice/views/order.py +++ b/boxoffice/views/order.py @@ -439,7 +439,7 @@ def payment(order): invoice_no=order.invoice_no, ), ) - if app.config['TELEGRAM_APIKEY'] and app.config['TELEGRAM_CHATID']: + 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},