Add Telegram message bot

This commit is contained in:
Kumi 2021-02-06 11:42:45 +00:00
parent c4154e6a02
commit 39901cd15f
2 changed files with 31 additions and 5 deletions

View file

@ -7,7 +7,7 @@ import random
from dbsettings.views import getValue from dbsettings.views import getValue
from buyer.models import Card from buyer.models import Card
import time import time
from smsauth.views import sendSMS from smsauth.views import sendMessage, SELLER
def getCard(allow_buy=False): def getCard(allow_buy=False):
unused = Card.objects.filter(delivered=None) # pylint: disable=E1101 unused = Card.objects.filter(delivered=None) # pylint: disable=E1101
@ -75,4 +75,4 @@ def buyCard():
def sendStatus(): def sendStatus():
status = len(Card.objects.filter(delivered=None)) 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)

View file

@ -4,8 +4,12 @@ from datetime import timedelta
from smsauth.models import Token from smsauth.models import Token
from dbsettings.views import getValue from dbsettings.views import getValue
from kumisms import KumiSMS from kumisms import KumiSMS
import telegram
import random import random
BUYER = 0
SELLER = 1
def generateToken(): def generateToken():
return str(random.SystemRandom().choice(range(999999))).zfill(6) return str(random.SystemRandom().choice(range(999999))).zfill(6)
@ -23,8 +27,30 @@ def useToken(token):
def requestToken(): def requestToken():
token = generateToken() token = generateToken()
storeToken(token) 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")) 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)