Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to Telegram #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lndrest
import lntxbot
import qr
import telegram

import utils
import importlib
Expand All @@ -22,6 +23,12 @@
led = "off"
logger = logging.getLogger("MAIN")

# Extend logging with Telegram if we got a bot_key
if config.conf["telegram"]["bot_key"]:
telegram_handler=telegram.TelegramHandler()
telegram_handler.setLevel(logging.INFO)
logger.addHandler(telegram_handler)


def softreset():
"""Displays startup screen and deletes fiat amount
Expand All @@ -30,7 +37,7 @@ def softreset():
config.SATS = 0
config.FIAT = 0
if config.COINCOUNT > 0:
logger.info("%s Coin(s) and XX Bill(s) added", config.COINCOUNT)
logger.debug("%s Coin(s) and XX Bill(s) added", config.COINCOUNT)
config.COINCOUNT = 0
# Turn off button LED
GPIO.output(13, GPIO.LOW)
Expand Down Expand Up @@ -159,7 +166,7 @@ def coins_inserted():
if config.FIAT == 0:
config.BTCPRICE = utils.get_btc_price(config.conf["atm"]["cur"])
config.SATPRICE = math.floor((1 / (config.BTCPRICE * 100)) * 100000000)
logger.info("Satoshi price updated")
logger.debug("Satoshi price updated")

if config.PULSES == 2:
config.FIAT += 0.02
Expand Down Expand Up @@ -215,7 +222,7 @@ def coins_inserted():
# Turn on the LED after first coin
GPIO.output(13, GPIO.HIGH)
led = "on"
logger.info("Button-LED turned on (if connected)")
logger.debug("Button-LED turned on (if connected)")


def monitor_coins_and_button():
Expand Down Expand Up @@ -316,12 +323,20 @@ def main():

# Display startup startup_screen
display.update_startup_screen()
lastupdate=time.time()


setup_coin_acceptor()

while True:
monitor_coins_and_button()

# Update the homescreen (and thus the time) every 60s
if ( time.time() - lastupdate ) > 60:
display.update_startup_screen()
lastupdate=time.time()



if __name__ == "__main__":
while True:
Expand Down
7 changes: 7 additions & 0 deletions example_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ macaroon =
# base64 encoded lntxbot api credentials
url =
creds =

[telegram]
# Create a bot using @botfather (/newbot). Give key below:
bot_key =
# Your own user ID to send messages to ( @lightningwatchbot
# can give you this if you type /referral )
user_id =
50 changes: 50 additions & 0 deletions telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python3

import json
import logging
from urllib import request, parse

import config
import utils

# import display
display_config = config.conf["atm"]["display"]
display = getattr(__import__("displays", fromlist=[display_config]), display_config)

logger = logging.getLogger("LNTXBOT")

class TelegramHandler(logging.StreamHandler):
"""Allow for logging to Telegram
"""
def emit(self,record):

self.send_message(record.getMessage())

def send_message(self,message):
"""Send a message over Telegram
"""
logger.info("Sending message [{}]".format(message))

bot_key=config.conf["telegram"]["bot_key"]
user_id=config.conf["telegram"]["user_id"]

# Message object
data = {
"chat_id": user_id,
"text": message,
}

data = json.dumps(data)
data = str(data)
data = data.encode('utf-8')

# Post Method is invoked if data != None
req = request.Request(
"https://api.telegram.org/bot{bot_key}/sendMessage".format(bot_key=bot_key),
data=data,
headers={
"Content-Type":"application/json",
})

# Response
resp = request.urlopen(req)