er1cw00 / CryptoBot.go

Telegram @CryptoBot Golang SDK to work with API

Home Page:https://t.me/CryptoBot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CryptoBot SDK Golang

Go Reference Go Report Card Tests License

Convenient SDK for @CryptoBot

Use it if you would like to accept payments in cryptocurrency through your Telegram Bot with Golang.

Installation

go get github.com/arthurshafikov/cryptobot-sdk-golang

Quick Start

import "github.com/arthurshafikov/cryptobot-sdk-golang/cryptobot"

func main() {
	client := cryptobot.NewClient(cryptobot.Options{
		APIToken: "<YOUR_API_KEY>",
	})

	appInfo, err := client.GetMe()
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Printf(
		"AppID - %v, Name - %s, PaymentProcessingBotUsername - %s \n",
		appInfo.AppID,
		appInfo.Name,
		appInfo.PaymentProcessingBotUsername,
	)
}

Examples

invoice, err := client.CreateInvoice(cryptobot.CreateInvoiceRequest{
    Asset:          cryptobot.USDT,
    Amount:         "125.50",
    Description:    "Description for the user",
    HiddenMessage:  "After invoice is paid user will see this message",
    PaidBtnName:    "", // optional. one of these viewItem, openChannel, openBot, callback
    PaidBtnUrl:     "", // URL to be opened when the PaidBtn is pressed
    Payload:        "any payload we need in out application",
    AllowComments:  false,  // Allow a user to add a comment to the payment. Default is true
    AllowAnonymous: false,  // Allow a user to pay the invoice anonymously. Default is true.
    ExpiresIn:      60 * 5, // invoice will expire in 5 minutes
})
if err != nil {
    log.Fatalln(err)
}
balance, err := client.GetBalance()
if err != nil {
    log.Fatalln(err)
}

for _, asset := range balance {
    fmt.Printf("Currency - %s, available - %s\n", asset.CurrencyCode, asset.Available)
}
currencies, err := client.GetCurrencies()
if err != nil {
    log.Fatalln(err)
}

for _, currency := range currencies {
    fmt.Printf(
        "Code - %s, Decimals - %v, IsBlockchain - %v, IsFiat - %v, IsStablecoin - %v, Name - %s, Url - %s \n",
        currency.Code,
        currency.Decimals,
        currency.IsBlockchain,
        currency.IsFiat,
        currency.IsStablecoin,
        currency.Name,
        currency.Url,
    )
}
exchangeRates, err := client.GetExchangeRates()
if err != nil {
    log.Fatalln(err)
}

for _, exchangeRate := range exchangeRates {
    fmt.Printf(
        "IsValid - %v, Rate - %v, Source - %v, Target - %v\n",
        exchangeRate.IsValid,
        exchangeRate.Rate,
        exchangeRate.Source,
        exchangeRate.Target,
    )
}
// GetInvoicesRequest argument is completely optional here
invoices, err := client.GetInvoices(&cryptobot.GetInvoicesRequest{
    Asset:      cryptobot.USDT,
    InvoiceIDs: "1,2,3",
    Status:     "active",
    Offset:     0,
    Count:      20,
})
if err != nil {
    log.Fatalln(err)
}
transfer, err := client.Transfer(cryptobot.TransferRequest{
    UserID:                  1,
    Asset:                   cryptobot.TON,
    Amount:                  "10.5",
    SpendID:                 "",
    Comment:                 "Debt",
    DisableSendNotification: false,
})
if err != nil {
    log.Fatalln(err)
}

fmt.Printf(
    "ID - %v, UserID - %s, Status - %s, Amount - %s, Asset - %s, Comment - %s, CompletedAt - %s \n",
    transfer.ID,
    transfer.UserID,
    transfer.Status,
    transfer.Amount,
    transfer.Asset,
    transfer.Comment,
    transfer.CompletedAt,
)
requestBody := []byte(`someJSONRequestBodyContent`)

webhookUpdate, err := cryptobot.ParseWebhookUpdate(requestBody)
if err != nil {
    log.Fatalln(err)
}

switch webhookUpdate.UpdateType {
case cryptobot.InvoicePaidWebhookUpdateType:
    invoice, err := cryptobot.ParseInvoice(requestBody)
    if err != nil {
        log.Fatalln(err)
    }

    if invoice.Status == cryptobot.InvoiceActiveStatus {
        showInvoiceInfo(invoice)
    }
default:
    log.Fatalln("unsupported webhook update type " + webhookUpdate.UpdateType)
}

Documentation

Check out this repository documentation

Check out official CryptoBot documentation

Troubleshooting

Feel free to create Issues and even suggest your own PRs

About

Telegram @CryptoBot Golang SDK to work with API

https://t.me/CryptoBot

License:MIT License


Languages

Language:Go 99.9%Language:Makefile 0.1%