Skip to content
This repository was archived by the owner on Dec 31, 2024. It is now read-only.

arthurshafikov/cryptobot-sdk-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

๐Ÿ—ƒ The package is archived and won't be updated at any time in the future

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published