mirror of
https://github.com/RoboSats/robosats.git
synced 2024-09-11 14:05:51 +03: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.messages import Telegram
|
||||
from api.utils import get_tor_session
|
||||
from decouple import config
|
||||
import requests
|
||||
import time
|
||||
@ -14,6 +15,8 @@ class Command(BaseCommand):
|
||||
bot_token = config('TELEGRAM_TOKEN')
|
||||
updates_url = f'https://api.telegram.org/bot{bot_token}/getUpdates'
|
||||
|
||||
session = get_tor_session()
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Infinite loop to check for telegram updates.
|
||||
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}
|
||||
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:
|
||||
continue
|
||||
for result in response['result']:
|
||||
|
@ -42,9 +42,9 @@ class Telegram():
|
||||
order = Order.objects.get(maker=user)
|
||||
print(str(order.id))
|
||||
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:
|
||||
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)
|
||||
return
|
||||
|
||||
@ -55,9 +55,9 @@ class Telegram():
|
||||
taker_nick = order.taker.username
|
||||
site = config('HOST_NAME')
|
||||
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:
|
||||
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)
|
||||
return
|
47
api/utils.py
47
api/utils.py
@ -1,13 +1,18 @@
|
||||
import requests, ring, os
|
||||
from decouple import config
|
||||
import numpy as np
|
||||
import requests
|
||||
|
||||
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 = {}
|
||||
|
||||
|
||||
@ring.dict(market_cache, expire=3) # keeps in cache for 3 seconds
|
||||
def get_exchange_rates(currencies):
|
||||
"""
|
||||
@ -16,6 +21,8 @@ def get_exchange_rates(currencies):
|
||||
Returns the median price list.
|
||||
"""
|
||||
|
||||
session = get_tor_session()
|
||||
|
||||
APIS = config("MARKET_PRICE_APIS",
|
||||
cast=lambda v: [s.strip() for s in v.split(",")])
|
||||
|
||||
@ -23,7 +30,7 @@ def get_exchange_rates(currencies):
|
||||
for api_url in APIS:
|
||||
try: # If one API is unavailable pass
|
||||
if "blockchain.info" in api_url:
|
||||
blockchain_prices = requests.get(api_url).json()
|
||||
blockchain_prices = session.get(api_url).json()
|
||||
blockchain_rates = []
|
||||
for currency in currencies:
|
||||
try: # If a currency is missing place a None
|
||||
@ -34,7 +41,7 @@ def get_exchange_rates(currencies):
|
||||
api_rates.append(blockchain_rates)
|
||||
|
||||
elif "yadio.io" in api_url:
|
||||
yadio_prices = requests.get(api_url).json()
|
||||
yadio_prices = session.get(api_url).json()
|
||||
yadio_rates = []
|
||||
for currency in currencies:
|
||||
try:
|
||||
@ -75,8 +82,6 @@ def get_lnd_version():
|
||||
|
||||
|
||||
robosats_commit_cache = {}
|
||||
|
||||
|
||||
@ring.dict(robosats_commit_cache, expire=3600)
|
||||
def get_commit_robosats():
|
||||
|
||||
@ -85,7 +90,6 @@ def get_commit_robosats():
|
||||
|
||||
return commit_hash
|
||||
|
||||
|
||||
premium_percentile = {}
|
||||
@ring.dict(premium_percentile, expire=300)
|
||||
def compute_premium_percentile(order):
|
||||
@ -104,29 +108,4 @@ def compute_premium_percentile(order):
|
||||
float(similar_order.last_satoshis) / float(similar_order.amount))
|
||||
|
||||
rates = np.array(rates)
|
||||
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
|
||||
return round(np.sum(rates < order_rate) / len(rates), 2)
|
@ -10,6 +10,7 @@ import QrReader from 'react-qr-reader'
|
||||
import PercentIcon from '@mui/icons-material/Percent';
|
||||
import BookIcon from '@mui/icons-material/Book';
|
||||
import QrCodeScannerIcon from '@mui/icons-material/QrCodeScanner';
|
||||
import SendIcon from '@mui/icons-material/Send';
|
||||
|
||||
function getCookie(name) {
|
||||
let cookieValue = null;
|
||||
@ -93,7 +94,7 @@ export default class TradeBox extends Component {
|
||||
<DialogContent>
|
||||
<DialogContentText id="alert-dialog-description">
|
||||
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,
|
||||
while the dispute loser will lose the bond.
|
||||
</DialogContentText>
|
||||
@ -264,11 +265,20 @@ export default class TradeBox extends Component {
|
||||
<Typography component="body2" variant="body2" align="left">
|
||||
<p>Be patient while robots check the book.
|
||||
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
|
||||
return to you (no action needed).</p>
|
||||
</Typography>
|
||||
</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 */}
|
||||
<Divider/>
|
||||
<ListItem>
|
||||
@ -446,7 +456,7 @@ export default class TradeBox extends Component {
|
||||
<Grid item xs={12} align="left">
|
||||
<Typography component="body2" variant="body2">
|
||||
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
|
||||
as possible to ensure a fair outcome. Max 5000 chars.
|
||||
</Typography>
|
||||
|
File diff suppressed because one or more lines are too long
@ -22,4 +22,5 @@ robohash==1.1
|
||||
scipy==1.8.0
|
||||
gunicorn==20.1.0
|
||||
psycopg2==2.9.3
|
||||
SQLAlchemy==1.4.31
|
||||
SQLAlchemy==1.4.31
|
||||
requests[socks]
|
Loading…
Reference in New Issue
Block a user