initial commit to this copied repo.

This commit is contained in:
Jens Noack (z003u7dz) 2025-02-12 10:07:58 +01:00
commit 019f8768cd
71 changed files with 6812 additions and 0 deletions

View file

@ -0,0 +1,5 @@
This is a simple telegram bot to notify you when the atm wallet balance is too low, or somebody did use it.
1. Set the according api keys in the main.py file.
2. Install python and the requests module (pip install requests)
3. Run the bot with 'python3 main.py' on any server or 24/7 online computer

View file

@ -0,0 +1,54 @@
import requests
from time import sleep
url = "https://legend.lnbits.com/api/v1/wallet" # your lnbits instance URL
api_key = "ABCDEFG" # your lnbits wallet api key (not admin!)
bot_token = '123123123:12312jn3123123kjn123f' # telegram bot token, create bot with @botfather
chat_id = '12312313123' # your telegram chat id, contact a chat id bot to get it
min_balance = 1000 # minimum balance at which you want to get notified
refresh_interval = 5600 # refresh interval in which tha balances are fetched in seconds
def get_wallet_balance():
headers = {
"X-Api-Key": api_key
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
return data["balance"]
else:
return int(-1)
def send_telegram_message(token, chat_id, message):
url = f'https://api.telegram.org/bot{token}/sendMessage'
data = {'chat_id': chat_id, 'text': message}
response = requests.post(url, data=data)
return response.json()
def bot():
previous_balance = 0
while (True):
balance = get_wallet_balance()
if balance == -1:
print("Balance check failed.")
sleep(3600)
continue
else:
balance = int(balance/1000)
difference = previous_balance - balance
if difference > 1:
message = f"{difference} Sats have been withdrawn!"
send_telegram_message(bot_token, chat_id, message)
elif balance < min_balance:
message = f"Only {balance} Sats left in ATM, refill!"
send_telegram_message(bot_token, chat_id, message)
previous_balance = balance
sleep(refresh_interval)
bot()

View file

@ -0,0 +1,6 @@
LNBITS_URL=
MIN_BALANCE=30000
REFRESH_INTERVAL=300
LNBITS_API_KEY=
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID=

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,18 @@
[package]
name = "atmbot_rust"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
reqwest = { version = "0.12.5", features = ["blocking", "json"] }
serde_json = "1.0.120"
[profile.release]
lto = true
opt-level = 3
strip = true
codegen-units = 1
debug = false

View file

@ -0,0 +1,6 @@
This is a simple telegram bot to notify you when the atm wallet balance is too low, or somebody did use it.
1. Set the according api keys in the .env file.
2. Install rust https://www.rust-lang.org/tools/install
3. Compile the bot with cargo build --release
4. Run the bot, it is located in ./target/release/atmbot

View file

@ -0,0 +1,82 @@
use dotenv::dotenv;
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderValue};
use serde_json::Value;
use std::env;
use std::thread::sleep;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenv().ok();
let url = env::var("LNBITS_URL").expect("URL not set in .env file");
let min_balance: i64 = env::var("MIN_BALANCE")
.expect("MIN_BALANCE not set in .env file")
.parse()?;
let refresh_interval: u64 = env::var("REFRESH_INTERVAL")
.expect("REFRESH_INTERVAL not set in .env file")
.parse()?;
let api_key = env::var("LNBITS_API_KEY").expect("LNBITS_API_KEY not set in .env file");
let bot_token =
env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set in .env file");
let chat_id = env::var("TELEGRAM_CHAT_ID").expect("TELEGRAM_CHAT_ID not set in .env file");
let mut previous_balance: i64 = 0;
loop {
match get_wallet_balance(&api_key, &url) {
Ok(balance) => {
let balance = balance / 1000;
let difference = previous_balance - balance;
if difference > 1 {
let message = format!("{} Sats have been withdrawn!", difference);
send_telegram_message(&bot_token, &chat_id, &message)?;
} else if balance < min_balance {
let message = format!("Only {} Sats left in ATM, refill!", balance);
send_telegram_message(&bot_token, &chat_id, &message)?;
}
previous_balance = balance;
}
Err(_) => {
println!("Balance check failed.");
sleep(Duration::from_secs(3600));
continue;
}
}
sleep(Duration::from_secs(refresh_interval));
}
}
fn get_wallet_balance(api_key: &str, url: &str) -> Result<i64, Box<dyn std::error::Error>> {
let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert("X-Api-Key", HeaderValue::from_str(api_key)?);
let response = client.get(url).headers(headers).send()?;
if response.status().is_success() {
let data: Value = response.json()?;
Ok(data["balance"]
.as_i64()
.expect("Failed to unwrap balance from returned JSON"))
} else {
Err(format!("Failed to get balance, status code: {}", response.status()).into())
}
}
fn send_telegram_message(
token: &str,
chat_id: &str,
message: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let telegram_url: String = format!("https://api.telegram.org/bot{}/sendMessage", token);
let params = [("chat_id", chat_id), ("text", message)];
let client = Client::new();
client.post(telegram_url).form(&params).send()?;
Ok(())
}