ebeneditos / telegram.bot

Develop a Telegram Bot with R
https://ebeneditos.github.io/telegram.bot/
GNU General Public License v3.0
107 stars 24 forks source link

Bot Payments API #29

Closed RKonstantinR closed 1 year ago

RKonstantinR commented 1 year ago

Hi, is it possible to use this package to build a telegram bot with payments (need metod: SendInvoice, PreCheckoutQuery, SuccessfulPayment)?

ebeneditos commented 1 year ago

Hi, that could be done but the feature is not developed right now, unfortunately don't have the time to do it but happy to review and provide guidance on pull requests.

RKonstantinR commented 1 year ago

Thanks for reply. Unfortunately my skills are not enough for development complete pull request. But I try to write a custom method and handler and the payment goes through, but the bot crashes when bot processing update with successful payment message. I get error: handler$check_update(update)) { : argument is of length zero

I think problem with my handler config (check_update and handle_update). How can i fix it?

My code:

library(telegram.bot)
library(glue)
library(httr)

bot_token <- "your_bot_token"

provider_token <- "your:TEST:token"

updater <- Updater(token = bot_token)

currency <- "your_currency_code"

## Start
start <- function(bot, update) {

    # create keyboard
    RKM <- ReplyKeyboardMarkup(
        keyboard = list(
            list(
                KeyboardButton(text = "donate")
            )
        ),
        resize_keyboard = TRUE,
        one_time_keyboard = TRUE
        )

    # send keyboard
    bot$sendMessage(update$message$chat_id,
                    text = 'Command', 
                    reply_markup = RKM)
    }

## Send invoice
send_invoice <- function(bot, update) {

    chat_id <- update$from_chat_id()

    title <- "Title"

    desc <- "Detail"

    payload <- "specialItem-001"

    prices <- '[{"label": "Payment", "amount": 24900}]'

    invoice <- glue("https://api.telegram.org/bot{bot_token}/sendInvoice?chat_id={chat_id}&title={title}&description={desc}&payload={payload}&provider_token={provider_token}&currency={currency}&prices={prices}")

    httr::POST(
        url = invoice
        )
    }

## Accept pre checkout query
pre_checkout <- function(bot, update) {

    chat_id <- update$pre_checkout_query$from$id

    invoice_id <- update$pre_checkout_query$id

    accept_invoice <- glue("https://api.telegram.org/bot{bot_token}/answerPreCheckoutQuery?pre_checkout_query_id={invoice_id}&ok=TRUE")

    httr::POST(
        url = accept_invoice
        )
    }

## View payment info
success_pay <- function(bot, update) { 
    str(update)
}

## Message filter
MessageFilters$invoice <- BaseFilter(function(message) {
    message$text == "donate"
    }
)

## Send RKM
h_start    <- CommandHandler('start', start)

## Invoice hendler
invoice_hendler <- MessageHandler(send_invoice, filters = MessageFilters$invoice)

## Pre checkout hendler
check_update <- function(update) {
    TRUE
}

handle_update <- function(update, dispatcher) {
    self$callback(dispatcher$bot, update)
}

pre_checkout_handler <- Handler(pre_checkout,
                                check_update = check_update,
                                handle_update = handle_update,
                                handlername = "FooHandler")

## Successful payment hendler 
payment_handler <- MessageHandler(success_pay, filters = MessageFilters$successful_payment)

## add hendler to dispatcher
updater <- updater + 
    h_start + 
    invoice_hendler + 
    pre_checkout_handler +
    payment_handler

## Start pooling
updater$start_polling(verbose = TRUE, clean = TRUE)
RKonstantinR commented 1 year ago

Solved.

MessageFilters$invoice <- BaseFilter(function(message) {
    if(!is.null(message) && is.null(message$successful_payment)) {
        message$text == "donate"
        } else {
        FALSE
        }
    }
)