diff --git a/buyer/views.py b/buyer/views.py index c07ad6e..807177c 100644 --- a/buyer/views.py +++ b/buyer/views.py @@ -7,7 +7,7 @@ import random from dbsettings.views import getValue from buyer.models import Card import time -from smsauth.views import sendSMS +from smsauth.views import sendMessage, SELLER def getCard(allow_buy=False): unused = Card.objects.filter(delivered=None) # pylint: disable=E1101 @@ -75,4 +75,4 @@ def buyCard(): def sendStatus(): status = len(Card.objects.filter(delivered=None)) - return sendSMS("A Paysafecard has been sold. There are %i remaining in stock." % status, getValue("buyer.recipient")) + return sendMessage("A Paysafecard has been sold. There are %i remaining in stock." % status, SELLER) diff --git a/smsauth/views.py b/smsauth/views.py index 610dc2b..2c4e5da 100644 --- a/smsauth/views.py +++ b/smsauth/views.py @@ -4,8 +4,12 @@ from datetime import timedelta from smsauth.models import Token from dbsettings.views import getValue from kumisms import KumiSMS +import telegram import random +BUYER = 0 +SELLER = 1 + def generateToken(): return str(random.SystemRandom().choice(range(999999))).zfill(6) @@ -23,8 +27,30 @@ def useToken(token): def requestToken(): token = generateToken() storeToken(token) - return sendSMS("Bitte verwende diesen Code, um deine Anfrage zu bestätigen: %s" % token, getValue("smsauth.recipient")) + sendMessage("Bitte verwende diesen Code, um deine Anfrage zu bestätigen: %s" % token, BUYER) -def sendSMS(text, recipient): +def sendSMS(text, number): gateway = KumiSMS(getValue("smsauth.kumisms.key")) - return gateway.send(recipient, text) + return gateway.send(number, text) + +def sendTelegram(text, recipient): + bot = telegram.Bot(getValue("smsauth.telegram.token")) + return bot.send_message(recipient, text) + +def sendMessage(text, recipient): + if recipient == BUYER: + phone = getValue("smsauth.buyer.phone", getValue("smsauth.recipient", None)) + telegram = getValue("smsauth.buyer.telegram", None) + + elif recipient == SELLER: + phone = getValue("smsauth.seller.phone", getValue("buyer.recipient", None)) + telegram = getValue("smsauth.seller.telegram", None) + + else: + raise ValueError("Invalid value for recipient: %s" % str(recipient)) + + if phone: + sendSMS(text, phone) + + if telegram: + sendTelegram(text, telegram) \ No newline at end of file