telegram-bot-ruby icon indicating copy to clipboard operation
telegram-bot-ruby copied to clipboard

sendInvoice doesn't support prices array

Open ivanovaleksey opened this issue 7 years ago • 9 comments

Hi @atipugin,

@yulgolem was playing with Bot API 3.0 and found a bug. He was sending parameters as

params = {
  chat_id: 1111111111,
  title: 'Invoice title',
  description: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod',
  payload: 'LWJK...',
  provider_token: STRIPE_TOKEN,
  start_parameter: 'WD6354FKJ...',
  currency: 'USD',
  prices: [{amount: 100, label: "PriceLabel_1" }, {amount: 200, label: "PriceLabel_2"}]
}

but Telegram returned an error ok: "false", error_code: "400", description: "Bad Request: prices are not specified")

However a request with the same parameters made via Postman succeed and invoice was sent to chat.

That made me think that something is wrong with request body encoding. sendInvoice has prices parameters which is an array of LabeledPrice.

After inspecting request body and decoding it I found that decoded request via Faraday looks like (I format it for readability):

chat_id=1111111111&
currency=USD&
description=Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod&
payload=LWJK...&
prices[][amount]=100&prices[][label]=PriceLabel_1&prices[][amount]=200&prices[][label]=PriceLabel_2&
provider_token=STRIPE_TOKEN&
start_parameter=WD6354FKJ...&
title=Invoice title

While Postman's decoded request body looks like:

chat_id=1111111111&
title=Invoice title&
description=Lorem+ipsum+dolor+sit+amet,+consectetur+adipisicing+elit,+sed+do+eiusmod&
payload=LWJK...&
provider_token=STRIPE_TOKEN&
currency=USD&
start_parameter=WD6354FKJ...&
prices=[{"amount":100,"label":"PriceLabel_1"},{"amount":200,"label":"PriceLabel_2"}]

Right now we are using url_encoded Faraday's middleware and payload is sent as application/x-www-form-urlencoded. I tried to use json middleware to send data as application/json and it seems to fix the problem.

I will provide PR for this, just the problem to be stated and known.

ivanovaleksey avatar May 29 '17 20:05 ivanovaleksey

Yep, I can confirm that. After using the gem from 0daf853969992b5a371b80847abc7b2723ef4481 the bug is gone and invoice is sent from bot to user.

yulgolem avatar Jun 06 '17 13:06 yulgolem

Can confirm the bug, but even with the mentioned commit it doesnt seem to work.

b1nary avatar Dec 14 '17 20:12 b1nary

@b1nary, I will take a look.

ivanovaleksey avatar Dec 15 '17 06:12 ivanovaleksey

Confirm bug

@b1nary I checked ivanovaleksey's commit and it works fine.

vursen avatar Dec 28 '17 06:12 vursen

I have the same problem. How to pass prices to sendInvoice properly?

wwwwhiterabbit avatar Mar 01 '18 07:03 wwwwhiterabbit

@vladimirsazhin this is not implemented in the main repo yet. As it's said above you can try to use a fork version just to make if it works. Though I don't recommend to switch to it entirely. To merge it to the main repo we need to do a testing since request's content type changes from url_encoded to json. You can share your experience here if you will give to fork a try.

ivanovaleksey avatar Mar 01 '18 13:03 ivanovaleksey

How can I access the conn property of Telegram::Bot::Api and configure adapter without forking the gem? Is it possible?

wwwwhiterabbit avatar Mar 19 '18 18:03 wwwwhiterabbit

So I found temporary solution.

prices = JSON.generate([{label: 'Foo', amount: 2000}, {label: 'Bar', amount: 2400}])
bot.api.send_invoice(..., prices: prices)

wwwwhiterabbit avatar Mar 19 '18 18:03 wwwwhiterabbit

{ "ok": false, "error_code": 400, "description": "Bad Request: expected Array of labeled prices" }

This error show me! How to fix it

okhun9494 avatar Jan 09 '20 05:01 okhun9494

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Feb 19 '23 15:02 github-actions[bot]