STORY
WebLN chatbot v2 discussion summary
AUTHOR
Joined 2022.06.04
DATE
VOTES
sats
COMMENTS

WebLN chatbot v2 discussion summary

We had a discussion about how we can implement the pay-per-package webln chatbot app for the workshop, and we came to a nice working solution.

Here is how the flow will look like for the user:

1- The user types some prompt in the text input

2- User hits Enter

3- On the client app, we check to see if we have a "payment token" stored somewhere:

-> If Yes: We just make the request to the '/chat' api & we include the token in some header

-> If No: We show the user a "Pick a package" modal. Upon clicking on a package, we a request to the '/chat' api with some query parameter like: "?package=1".

The API will here generate an invoice with an amount depending on the package the user selected. & finally return a '402' response with this invoice.

4- When the client receives the response, it shows a payment modal with the invoice returned.

When the user pays it. It sends the request to the '/chat' api again but this time with the payment token (token from API + preimage)

5- The API validates the token, makes sure it has enough remaining credits (requests/tokens/time/...etc) by checking the DB

6- The API makes the request to OpenAI API, updates the token's remaining credits in DB, & returns the result to the user.

Notes:

- If the user make an API request to '/chat' without a payment token & without a selected package, we assume a default package & generate an invoice & return it.

- In step 3, the "No" route, I feel that this should be done by a different API.

Because here we are just requesting the API to generate an invoice for some package we chose.

We know definitely that it will return 402. So I feel it makes more sense to have a '/get-invoice?package=1' endpoint instead.

- Of course we can keep the functionality of generating an invoice & returning 402 on the '/chat' endpoint for the cases where this endpoint is being requested automatically (by a machine for example)

- I created a new branch on the workshop repo where we can work on this new version.

Here is the link: https://github.com/peakshift/webln-chatbot/tree/pay-per-package

The file that currently has the '/chat' endpoint is located at api/functions/chat.ts