Implement helper function to send messages on IRC

This commit is contained in:
Mark Veidemanis 2022-01-01 16:39:36 +00:00
parent 86271891a7
commit 1227a16b72
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 22 additions and 21 deletions

View File

@ -94,7 +94,7 @@ class Agora(object):
if reference not in current_trades: if reference not in current_trades:
current_trades.append(reference) current_trades.append(reference)
# Let us know there is a new trade # Let us know there is a new trade
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: {buyer} {amount}{currency} {amount_xmr}XMR") self.irc.sendmsg(f"AUTO {reference}: {buyer} {amount}{currency} {amount_xmr}XMR")
# Note that we have seen this reference # Note that we have seen this reference
self.last_dash.add(reference) self.last_dash.add(reference)
@ -161,13 +161,13 @@ class Agora(object):
for user, message in messages_tmp[reference]: for user, message in messages_tmp[reference]:
if reference in self.last_messages: if reference in self.last_messages:
if not [user, message] in self.last_messages[reference]: if not [user, message] in self.last_messages[reference]:
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: ({user}) {message}") self.irc.sendmsg(f"AUTO {reference}: ({user}) {message}")
# Append sent messages to last_messages so we don't send them again # Append sent messages to last_messages so we don't send them again
self.last_messages[reference].append([user, message]) self.last_messages[reference].append([user, message])
else: else:
self.last_messages[reference] = [[user, message]] self.last_messages[reference] = [[user, message]]
for x in messages_tmp[reference]: for x in messages_tmp[reference]:
self.irc.client.msg(self.irc.client.channel, f"NEW {reference}: ({user}) {message}") self.irc.sendmsg(f"NEW {reference}: ({user}) {message}")
# Purge old trades from cache # Purge old trades from cache
for ref in list(self.last_messages): # We're removing from the list on the fly for ref in list(self.last_messages): # We're removing from the list on the fly

View File

@ -188,6 +188,16 @@ class IRCBotFactory(protocol.ClientFactory):
def set_tx(self, tx): def set_tx(self, tx):
self.tx = tx self.tx = tx
def sendmsg(self, msg):
"""
Passthrough function to send a message to the channel.
"""
if self.client:
self.client.msg(self.client.channel, msg)
else:
self.log.error("Trying to send a message without connected client: {msg}", msg=msg)
return
def buildProtocol(self, addr): def buildProtocol(self, addr):
""" """
Custom override for the Twisted buildProtocol so we can access the Protocol instance. Custom override for the Twisted buildProtocol so we can access the Protocol instance.

View File

@ -75,14 +75,12 @@ class Transactions(object):
self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2)) self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2))
r.hmset(f"tx.{txid}", to_store) r.hmset(f"tx.{txid}", to_store)
self.irc.client.msg( self.irc.sendmsg(f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}")
self.irc.client.channel, f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}"
)
# Try getting the trade by the reference ID given # Try getting the trade by the reference ID given
stored_trade = self.get_ref(reference) stored_trade = self.get_ref(reference)
if not stored_trade: if not stored_trade:
self.log.info(f"No reference in DB for {reference}", reference=reference) self.log.info(f"No reference in DB for {reference}", reference=reference)
self.irc.client.msg(self.irc.client.channel, f"No reference in DB for {reference}") self.irc.sendmsg(f"No reference in DB for {reference}")
return return
amount = float(amount) amount = float(amount)
@ -90,30 +88,25 @@ class Transactions(object):
# Make sure it was sent in the expected currency # Make sure it was sent in the expected currency
if not stored_trade["currency"] == currency: if not stored_trade["currency"] == currency:
self.irc.client.msg(self.irc.client.channel, f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}") self.irc.sendmsg(f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}")
return return
# Make sure the expected amount was sent # Make sure the expected amount was sent
if not stored_trade["amount"] == amount: if not stored_trade["amount"] == amount:
# If the amount does not match exactly, get the min and max values for our given acceptable margins for trades # If the amount does not match exactly, get the min and max values for our given acceptable margins for trades
min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount) min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount)
self.irc.client.msg( self.irc.sendmsg(f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}")
self.irc.client.channel, f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}"
)
if not min_amount < stored_trade["amount"] < max_amount: if not min_amount < stored_trade["amount"] < max_amount:
self.irc.client.msg( self.irc.sendmsg(f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}")
self.irc.client.channel,
f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}",
)
return return
# Make sure the account type was Revolut, as these are completed instantly # Make sure the account type was Revolut, as these are completed instantly
if not account_type == "revolut": if not account_type == "revolut":
self.irc.client.msg(self.irc.client.channel, f"Account type is not Revolut: {account_type}") self.irc.sendmsg(f"Account type is not Revolut: {account_type}")
return return
self.irc.client.msg(self.irc.client.channel, f"All checks passed, releasing funds for {stored_trade['id']} / {reference}") self.irc.sendmsg(f"All checks passed, releasing funds for {stored_trade['id']} / {reference}")
rtrn = self.agora.release_funds(stored_trade["id"]) rtrn = self.agora.release_funds(stored_trade["id"])
self.agora.agora.contact_message_post(stored_trade["id"], "Thanks! Releasing now :)") self.agora.agora.contact_message_post(stored_trade["id"], "Thanks! Releasing now :)")
self.irc.client.msg(self.irc.client.channel, dumps(rtrn)) self.irc.sendmsg(dumps(rtrn))
def new_trade(self, trade_id, buyer, currency, amount, amount_xmr): def new_trade(self, trade_id, buyer, currency, amount, amount_xmr):
""" """
@ -123,8 +116,6 @@ class Transactions(object):
reference = "".join(choices(ascii_uppercase, k=5)) reference = "".join(choices(ascii_uppercase, k=5))
reference = f"XMR-{reference}" reference = f"XMR-{reference}"
existing_ref = r.get(f"trade.{trade_id}.reference") existing_ref = r.get(f"trade.{trade_id}.reference")
# if existing_ref:
# self.irc.client.msg(self.irc.client.channel, f"Existing reference for {trade_id}: {existing_ref.decode('utf-8')}")
if not existing_ref: if not existing_ref:
r.set(f"trade.{trade_id}.reference", reference) r.set(f"trade.{trade_id}.reference", reference)
to_store = { to_store = {
@ -137,7 +128,7 @@ class Transactions(object):
} }
self.log.info("Storing trade information: {info}", info=str(to_store)) self.log.info("Storing trade information: {info}", info=str(to_store))
r.hmset(f"trade.{reference}", to_store) r.hmset(f"trade.{reference}", to_store)
self.irc.client.msg(self.irc.client.channel, f"Generated reference for {trade_id}: {reference}") self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}")
if settings.Agora.Send == "1": if settings.Agora.Send == "1":
self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please use reference code: {reference}") self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please use reference code: {reference}")
if existing_ref: if existing_ref: