Skip to content

Instantly share code, notes, and snippets.

@w0w
Created October 16, 2024 12:05
Show Gist options
  • Save w0w/24360e632006d278480ad6899daf8a79 to your computer and use it in GitHub Desktop.
Save w0w/24360e632006d278480ad6899daf8a79 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
telegramBotToken = "your_telegram_bot_token" // Replace with your Telegram bot token
telegramChatID = "@your_channel_username" // Replace with your Telegram channel username
transactionsURL = "https://lannaedges.radicalxchange.org/transactions"
checkInterval = 60 * time.Second // Time interval between checks
lastTransactionFile = "last_transaction.txt" // File to store the last posted transaction
)
// Transaction structure
type Transaction struct {
Date string
From string
To string
Amount string
}
// Function to scrape transactions from the web page
func scrapeTransactions() ([]Transaction, error) {
var transactions []Transaction
// Request the page
res, err := http.Get(transactionsURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch transactions page: %v", err)
}
defer res.Body.Close()
// Parse the page
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to parse transactions page: %v", err)
}
// Example: Assuming the transactions are in a table
doc.Find("table tbody tr").Each(func(index int, row *goquery.Selection) {
cells := row.Find("td")
if cells.Length() > 3 {
transaction := Transaction{
Date: strings.TrimSpace(cells.Eq(0).Text()),
From: strings.TrimSpace(cells.Eq(1).Text()),
To: strings.TrimSpace(cells.Eq(2).Text()),
Amount: strings.TrimSpace(cells.Eq(3).Text()),
}
transactions = append(transactions, transaction)
}
})
return transactions, nil
}
// Function to send a transaction to the Telegram channel
func postTransactionToTelegram(bot *tgbotapi.BotAPI, transaction Transaction) error {
message := fmt.Sprintf("New Transaction:\nDate: %s\nFrom: %s\nTo: %s\nAmount: %s", transaction.Date, transaction.From, transaction.To, transaction.Amount)
msg := tgbotapi.NewMessageToChannel(telegramChatID, message)
_, err := bot.Send(msg)
return err
}
// Function to load the last posted transaction from a file
func loadLastTransaction() (*Transaction, error) {
data, err := ioutil.ReadFile(lastTransactionFile)
if err != nil {
if os.IsNotExist(err) {
// If the file doesn't exist, it's the first run
return nil, nil
}
return nil, err
}
parts := strings.Split(string(data), "|")
if len(parts) != 4 {
return nil, fmt.Errorf("invalid transaction data in file")
}
return &Transaction{
Date: parts[0],
From: parts[1],
To: parts[2],
Amount: parts[3],
}, nil
}
// Function to save the last posted transaction to a file
func saveLastTransaction(transaction Transaction) error {
data := fmt.Sprintf("%s|%s|%s|%s", transaction.Date, transaction.From, transaction.To, transaction.Amount)
return ioutil.WriteFile(lastTransactionFile, []byte(data), 0644)
}
func main() {
// Initialize Telegram bot
bot, err := tgbotapi.NewBotAPI(telegramBotToken)
if err != nil {
log.Fatalf("Failed to initialize Telegram bot: %v", err)
}
log.Printf("Bot authorized on account %s", bot.Self.UserName)
// Load the last posted transaction from file
lastPostedTransaction, err := loadLastTransaction()
if err != nil {
log.Fatalf("Failed to load last transaction: %v", err)
}
for {
// Scrape the transactions
transactions, err := scrapeTransactions()
if err != nil {
log.Printf("Error scraping transactions: %v", err)
time.Sleep(checkInterval)
continue
}
// Check if there is a new transaction
if len(transactions) > 0 {
newTransaction := transactions[0] // Assuming the first transaction is the latest
// Post the new transaction if it's different from the last posted one
if lastPostedTransaction == nil || newTransaction != *lastPostedTransaction {
err = postTransactionToTelegram(bot, newTransaction)
if err != nil {
log.Printf("Error sending message to Telegram: %v", err)
} else {
// Save the new transaction as the last posted one
err = saveLastTransaction(newTransaction)
if err != nil {
log.Printf("Error saving last transaction: %v", err)
} else {
lastPostedTransaction = &newTransaction
log.Printf("Posted new transaction: %+v", newTransaction)
}
}
}
}
// Wait before the next check
time.Sleep(checkInterval)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment