mirror of
https://github.com/RoboSats/robosats.git
synced 2025-02-22 05:09:01 +00:00
Add frontend enable telegram button. Torify all requests.
This commit is contained in:
parent
df320ea4d0
commit
d146f522f3
@ -2,6 +2,7 @@ from django.core.management.base import BaseCommand, CommandError
|
|||||||
|
|
||||||
from api.models import Profile
|
from api.models import Profile
|
||||||
from api.messages import Telegram
|
from api.messages import Telegram
|
||||||
|
from api.utils import get_tor_session
|
||||||
from decouple import config
|
from decouple import config
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
@ -14,6 +15,8 @@ class Command(BaseCommand):
|
|||||||
bot_token = config('TELEGRAM_TOKEN')
|
bot_token = config('TELEGRAM_TOKEN')
|
||||||
updates_url = f'https://api.telegram.org/bot{bot_token}/getUpdates'
|
updates_url = f'https://api.telegram.org/bot{bot_token}/getUpdates'
|
||||||
|
|
||||||
|
session = get_tor_session()
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
"""Infinite loop to check for telegram updates.
|
"""Infinite loop to check for telegram updates.
|
||||||
If it finds a new user (/start), enables it's taker found
|
If it finds a new user (/start), enables it's taker found
|
||||||
@ -25,7 +28,7 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
params = {'offset' : offset + 1 , 'timeout' : 5}
|
params = {'offset' : offset + 1 , 'timeout' : 5}
|
||||||
print(params)
|
print(params)
|
||||||
response = requests.get(self.updates_url, params=params).json()
|
response = self.session.get(self.updates_url, params=params).json()
|
||||||
if len(list(response['result'])) == 0:
|
if len(list(response['result'])) == 0:
|
||||||
continue
|
continue
|
||||||
for result in response['result']:
|
for result in response['result']:
|
||||||
|
@ -42,9 +42,9 @@ class Telegram():
|
|||||||
order = Order.objects.get(maker=user)
|
order = Order.objects.get(maker=user)
|
||||||
print(str(order.id))
|
print(str(order.id))
|
||||||
if lang == 'es':
|
if lang == 'es':
|
||||||
text = f'Hola {user.username}, te enviaré un mensaje cuando tu orden con ID {str(order.id)} haya sido tomada.'
|
text = f'Hola ⚡{user.username}⚡, Te enviaré un mensaje cuando tu orden con ID {str(order.id)} haya sido tomada.'
|
||||||
else:
|
else:
|
||||||
text = f"Hey {user.username}, I will send you a message when someone takes your order with ID {str(order.id)}."
|
text = f"Hey ⚡{user.username}⚡, I will send you a message when someone takes your order with ID {str(order.id)}."
|
||||||
cls.send_message(user, text)
|
cls.send_message(user, text)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -55,9 +55,9 @@ class Telegram():
|
|||||||
taker_nick = order.taker.username
|
taker_nick = order.taker.username
|
||||||
site = config('HOST_NAME')
|
site = config('HOST_NAME')
|
||||||
if lang == 'es':
|
if lang == 'es':
|
||||||
text = f'Tu orden con ID {order.id} ha sido tomada por {taker_nick}!🎉 Visita http://{site}/order/{order.id} para continuar.'
|
text = f'Tu orden con ID {order.id} ha sido tomada por {taker_nick}!🥳 Visita http://{site}/order/{order.id} para continuar.'
|
||||||
else:
|
else:
|
||||||
text = f'Your order with ID {order.id} was taken by {taker_nick}!🎉 Visit http://{site}/order/{order.id} to proceed with the trade.'
|
text = f'Your order with ID {order.id} was taken by {taker_nick}!🥳 Visit http://{site}/order/{order.id} to proceed with the trade.'
|
||||||
|
|
||||||
cls.send_message(user, text)
|
cls.send_message(user, text)
|
||||||
return
|
return
|
45
api/utils.py
45
api/utils.py
@ -1,13 +1,18 @@
|
|||||||
import requests, ring, os
|
import requests, ring, os
|
||||||
from decouple import config
|
from decouple import config
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import requests
|
||||||
|
|
||||||
from api.models import Order
|
from api.models import Order
|
||||||
from secrets import token_urlsafe
|
|
||||||
|
def get_tor_session():
|
||||||
|
session = requests.session()
|
||||||
|
# Tor uses the 9050 port as the default socks port
|
||||||
|
session.proxies = {'http': 'socks5://127.0.0.1:9050',
|
||||||
|
'https': 'socks5://127.0.0.1:9050'}
|
||||||
|
return session
|
||||||
|
|
||||||
market_cache = {}
|
market_cache = {}
|
||||||
|
|
||||||
|
|
||||||
@ring.dict(market_cache, expire=3) # keeps in cache for 3 seconds
|
@ring.dict(market_cache, expire=3) # keeps in cache for 3 seconds
|
||||||
def get_exchange_rates(currencies):
|
def get_exchange_rates(currencies):
|
||||||
"""
|
"""
|
||||||
@ -16,6 +21,8 @@ def get_exchange_rates(currencies):
|
|||||||
Returns the median price list.
|
Returns the median price list.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
session = get_tor_session()
|
||||||
|
|
||||||
APIS = config("MARKET_PRICE_APIS",
|
APIS = config("MARKET_PRICE_APIS",
|
||||||
cast=lambda v: [s.strip() for s in v.split(",")])
|
cast=lambda v: [s.strip() for s in v.split(",")])
|
||||||
|
|
||||||
@ -23,7 +30,7 @@ def get_exchange_rates(currencies):
|
|||||||
for api_url in APIS:
|
for api_url in APIS:
|
||||||
try: # If one API is unavailable pass
|
try: # If one API is unavailable pass
|
||||||
if "blockchain.info" in api_url:
|
if "blockchain.info" in api_url:
|
||||||
blockchain_prices = requests.get(api_url).json()
|
blockchain_prices = session.get(api_url).json()
|
||||||
blockchain_rates = []
|
blockchain_rates = []
|
||||||
for currency in currencies:
|
for currency in currencies:
|
||||||
try: # If a currency is missing place a None
|
try: # If a currency is missing place a None
|
||||||
@ -34,7 +41,7 @@ def get_exchange_rates(currencies):
|
|||||||
api_rates.append(blockchain_rates)
|
api_rates.append(blockchain_rates)
|
||||||
|
|
||||||
elif "yadio.io" in api_url:
|
elif "yadio.io" in api_url:
|
||||||
yadio_prices = requests.get(api_url).json()
|
yadio_prices = session.get(api_url).json()
|
||||||
yadio_rates = []
|
yadio_rates = []
|
||||||
for currency in currencies:
|
for currency in currencies:
|
||||||
try:
|
try:
|
||||||
@ -75,8 +82,6 @@ def get_lnd_version():
|
|||||||
|
|
||||||
|
|
||||||
robosats_commit_cache = {}
|
robosats_commit_cache = {}
|
||||||
|
|
||||||
|
|
||||||
@ring.dict(robosats_commit_cache, expire=3600)
|
@ring.dict(robosats_commit_cache, expire=3600)
|
||||||
def get_commit_robosats():
|
def get_commit_robosats():
|
||||||
|
|
||||||
@ -85,7 +90,6 @@ def get_commit_robosats():
|
|||||||
|
|
||||||
return commit_hash
|
return commit_hash
|
||||||
|
|
||||||
|
|
||||||
premium_percentile = {}
|
premium_percentile = {}
|
||||||
@ring.dict(premium_percentile, expire=300)
|
@ring.dict(premium_percentile, expire=300)
|
||||||
def compute_premium_percentile(order):
|
def compute_premium_percentile(order):
|
||||||
@ -105,28 +109,3 @@ def compute_premium_percentile(order):
|
|||||||
|
|
||||||
rates = np.array(rates)
|
rates = np.array(rates)
|
||||||
return round(np.sum(rates < order_rate) / len(rates), 2)
|
return round(np.sum(rates < order_rate) / len(rates), 2)
|
||||||
|
|
||||||
|
|
||||||
def get_telegram_context(user):
|
|
||||||
"""returns context needed to enable TG notifications"""
|
|
||||||
context = {}
|
|
||||||
if user.profile.telegram_enabled :
|
|
||||||
context['tg_enabled'] = True
|
|
||||||
else:
|
|
||||||
context['tg_enabled'] = False
|
|
||||||
|
|
||||||
if user.profile.telegram_token == None:
|
|
||||||
user.profile.telegram_token = token_urlsafe(15)
|
|
||||||
|
|
||||||
context['tg_token'] = user.profile.telegram_token
|
|
||||||
context['tg_bot_name'] = config("TELEGRAM_BOT_NAME")
|
|
||||||
|
|
||||||
return context
|
|
||||||
|
|
||||||
def send_telegram_notification(user, text):
|
|
||||||
bot_token=config('TELEGRAM_TOKEN')
|
|
||||||
chat_id = user.profile.telegram_chat_id
|
|
||||||
message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={text}'
|
|
||||||
response = requests.get(message_url).json()
|
|
||||||
print(response)
|
|
||||||
return
|
|
@ -10,6 +10,7 @@ import QrReader from 'react-qr-reader'
|
|||||||
import PercentIcon from '@mui/icons-material/Percent';
|
import PercentIcon from '@mui/icons-material/Percent';
|
||||||
import BookIcon from '@mui/icons-material/Book';
|
import BookIcon from '@mui/icons-material/Book';
|
||||||
import QrCodeScannerIcon from '@mui/icons-material/QrCodeScanner';
|
import QrCodeScannerIcon from '@mui/icons-material/QrCodeScanner';
|
||||||
|
import SendIcon from '@mui/icons-material/Send';
|
||||||
|
|
||||||
function getCookie(name) {
|
function getCookie(name) {
|
||||||
let cookieValue = null;
|
let cookieValue = null;
|
||||||
@ -93,7 +94,7 @@ export default class TradeBox extends Component {
|
|||||||
<DialogContent>
|
<DialogContent>
|
||||||
<DialogContentText id="alert-dialog-description">
|
<DialogContentText id="alert-dialog-description">
|
||||||
The RoboSats staff will examine the statements and evidence provided. You need to build
|
The RoboSats staff will examine the statements and evidence provided. You need to build
|
||||||
a complete case, as the staff cannot read the chat. You MUST provide a burner contact
|
a complete case, as the staff cannot read the chat. It is best to provide a burner contact
|
||||||
method with your statement. The satoshis in the trade escrow will be sent to the dispute winner,
|
method with your statement. The satoshis in the trade escrow will be sent to the dispute winner,
|
||||||
while the dispute loser will lose the bond.
|
while the dispute loser will lose the bond.
|
||||||
</DialogContentText>
|
</DialogContentText>
|
||||||
@ -264,11 +265,20 @@ export default class TradeBox extends Component {
|
|||||||
<Typography component="body2" variant="body2" align="left">
|
<Typography component="body2" variant="body2" align="left">
|
||||||
<p>Be patient while robots check the book.
|
<p>Be patient while robots check the book.
|
||||||
It might take some time. This box will ring 🔊 once a robot takes your order. </p>
|
It might take some time. This box will ring 🔊 once a robot takes your order. </p>
|
||||||
<p>Please note that if your premium is too high, or if your currency or payment
|
<p>Please note that if your premium is excessive, or your currency or payment
|
||||||
methods are not popular, your order might expire untaken. Your bond will
|
methods are not popular, your order might expire untaken. Your bond will
|
||||||
return to you (no action needed).</p>
|
return to you (no action needed).</p>
|
||||||
</Typography>
|
</Typography>
|
||||||
</ListItem>
|
</ListItem>
|
||||||
|
<Grid item xs={12} align="center">
|
||||||
|
{this.props.data.tg_enabled ?
|
||||||
|
<Typography color='primary' component="h6" variant="h6" align="center"> Telegram enabled</Typography>
|
||||||
|
:
|
||||||
|
<Button color="primary" component="a" target="_blank" href={"https://t.me/"+this.props.data.tg_bot_name+'?start='+this.props.data.tg_token}>
|
||||||
|
<SendIcon/>Enable Telegram Notifications
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
</Grid>
|
||||||
{/* TODO API sends data for a more confortable wait */}
|
{/* TODO API sends data for a more confortable wait */}
|
||||||
<Divider/>
|
<Divider/>
|
||||||
<ListItem>
|
<ListItem>
|
||||||
@ -446,7 +456,7 @@ export default class TradeBox extends Component {
|
|||||||
<Grid item xs={12} align="left">
|
<Grid item xs={12} align="left">
|
||||||
<Typography component="body2" variant="body2">
|
<Typography component="body2" variant="body2">
|
||||||
Please, submit your statement. Be clear and specific about what happened and provide the necessary
|
Please, submit your statement. Be clear and specific about what happened and provide the necessary
|
||||||
evidence. You MUST provide a burner email, XMPP or telegram username to follow up with the staff.
|
evidence. It is best to provide a burner email, XMPP or telegram username to follow up with the staff.
|
||||||
Disputes are solved at the discretion of real robots <i>(aka humans)</i>, so be as helpful
|
Disputes are solved at the discretion of real robots <i>(aka humans)</i>, so be as helpful
|
||||||
as possible to ensure a fair outcome. Max 5000 chars.
|
as possible to ensure a fair outcome. Max 5000 chars.
|
||||||
</Typography>
|
</Typography>
|
||||||
|
File diff suppressed because one or more lines are too long
@ -23,3 +23,4 @@ scipy==1.8.0
|
|||||||
gunicorn==20.1.0
|
gunicorn==20.1.0
|
||||||
psycopg2==2.9.3
|
psycopg2==2.9.3
|
||||||
SQLAlchemy==1.4.31
|
SQLAlchemy==1.4.31
|
||||||
|
requests[socks]
|
Loading…
Reference in New Issue
Block a user