Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
CarolinePascal commited on
feat(stripe): add support for STRIPE API
Browse files- app.py +59 -26
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from datetime import datetime, timedelta
|
|
| 5 |
|
| 6 |
import discord
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
from discord.ext import commands
|
| 9 |
|
| 10 |
|
|
@@ -12,6 +13,7 @@ from discord.ext import commands
|
|
| 12 |
MY_GUILD_ID = int(os.environ.get("GUILD_ID"))
|
| 13 |
MY_GUILD = discord.Object(id=MY_GUILD_ID)
|
| 14 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN")
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
class Bot(commands.Bot):
|
|
@@ -37,6 +39,27 @@ async def on_ready():
|
|
| 37 |
MIN_DATE = datetime(2025, 7, 9)
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
class DayButton(discord.ui.Button):
|
| 41 |
def __init__(self, day: int, row: int, disabled: bool = False):
|
| 42 |
super().__init__(
|
|
@@ -50,26 +73,8 @@ class DayButton(discord.ui.Button):
|
|
| 50 |
async def callback(self, interaction: discord.Interaction):
|
| 51 |
view: DatePickerView = self.view
|
| 52 |
purchase_date = datetime(view.year, view.month, self.day)
|
| 53 |
-
|
| 54 |
-
arrival_date = purchase_date + timedelta(days=90)
|
| 55 |
-
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
| 56 |
-
days_until = (arrival_date - today).days
|
| 57 |
-
|
| 58 |
-
if days_until < -14:
|
| 59 |
-
status = "β
Your Reachy Mini must already have been delivered! Check with your carrier or contact sales@pollen-robotics.com if you haven't received it."
|
| 60 |
-
elif days_until < 0:
|
| 61 |
-
status = "β³ The delivery seems to have been delayed a bit. It should arrive any day now β hang tight!"
|
| 62 |
-
elif days_until <= 7:
|
| 63 |
-
status = "π The delivery is right around the corner! Your Reachy Mini will be there very soon."
|
| 64 |
-
else:
|
| 65 |
-
status = f"π Your Reachy Mini is on its way! About **{days_until} days** to go."
|
| 66 |
-
|
| 67 |
await interaction.response.edit_message(
|
| 68 |
-
content=(
|
| 69 |
-
f"ποΈ Purchase date: **{purchase_date.strftime('%B %d, %Y')}**\n"
|
| 70 |
-
f"π¦ Expected Reachy Mini arrival: **{arrival_date.strftime('%B %d, %Y')}** (90 days after purchase)\n\n"
|
| 71 |
-
f"{status}"
|
| 72 |
-
),
|
| 73 |
view=None,
|
| 74 |
)
|
| 75 |
|
|
@@ -212,13 +217,41 @@ class DatePickerView(discord.ui.View):
|
|
| 212 |
|
| 213 |
|
| 214 |
@client.tree.command(name="eta", description="Find out when your Reachy Mini will arrive", guild=MY_GUILD)
|
| 215 |
-
async def eta(interaction: discord.Interaction):
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
|
| 224 |
def run_bot():
|
|
|
|
| 5 |
|
| 6 |
import discord
|
| 7 |
import gradio as gr
|
| 8 |
+
import stripe
|
| 9 |
from discord.ext import commands
|
| 10 |
|
| 11 |
|
|
|
|
| 13 |
MY_GUILD_ID = int(os.environ.get("GUILD_ID"))
|
| 14 |
MY_GUILD = discord.Object(id=MY_GUILD_ID)
|
| 15 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN")
|
| 16 |
+
stripe.api_key = os.environ.get("STRIPE_API_KEY")
|
| 17 |
|
| 18 |
|
| 19 |
class Bot(commands.Bot):
|
|
|
|
| 39 |
MIN_DATE = datetime(2025, 7, 9)
|
| 40 |
|
| 41 |
|
| 42 |
+
def format_eta_result(purchase_date: datetime) -> str:
|
| 43 |
+
arrival_date = purchase_date + timedelta(days=90)
|
| 44 |
+
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
|
| 45 |
+
days_until = (arrival_date - today).days
|
| 46 |
+
|
| 47 |
+
if days_until < -14:
|
| 48 |
+
status = "β
Your Reachy Mini must already have been delivered! Check with your carrier or contact sales@pollen-robotics.com if you haven't received it."
|
| 49 |
+
elif days_until < 0:
|
| 50 |
+
status = "β³ The delivery seems to have been delayed a bit. It should arrive any day now β hang tight!"
|
| 51 |
+
elif days_until <= 7:
|
| 52 |
+
status = "π The delivery is right around the corner! Your Reachy Mini will be there very soon."
|
| 53 |
+
else:
|
| 54 |
+
status = f"π Your Reachy Mini is on its way! About **{days_until} days** to go."
|
| 55 |
+
|
| 56 |
+
return (
|
| 57 |
+
f"ποΈ Purchase date: **{purchase_date.strftime('%B %d, %Y')}**\n"
|
| 58 |
+
f"π¦ Expected Reachy Mini arrival: **{arrival_date.strftime('%B %d, %Y')}** (90 days after purchase)\n\n"
|
| 59 |
+
f"{status}"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
class DayButton(discord.ui.Button):
|
| 64 |
def __init__(self, day: int, row: int, disabled: bool = False):
|
| 65 |
super().__init__(
|
|
|
|
| 73 |
async def callback(self, interaction: discord.Interaction):
|
| 74 |
view: DatePickerView = self.view
|
| 75 |
purchase_date = datetime(view.year, view.month, self.day)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
await interaction.response.edit_message(
|
| 77 |
+
content=format_eta_result(purchase_date),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
view=None,
|
| 79 |
)
|
| 80 |
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
@client.tree.command(name="eta", description="Find out when your Reachy Mini will arrive", guild=MY_GUILD)
|
| 220 |
+
async def eta(interaction: discord.Interaction, invoice: str = None):
|
| 221 |
+
if invoice:
|
| 222 |
+
await interaction.response.defer(ephemeral=True)
|
| 223 |
+
try:
|
| 224 |
+
inv = stripe.Invoice.retrieve(invoice)
|
| 225 |
+
except stripe.error.InvalidRequestError:
|
| 226 |
+
view = DatePickerView()
|
| 227 |
+
await interaction.followup.send(
|
| 228 |
+
"β Invoice not found β make sure you're entering the **invoice number** (not the order number). Please select your purchase date manually:",
|
| 229 |
+
view=view,
|
| 230 |
+
ephemeral=True,
|
| 231 |
+
)
|
| 232 |
+
return
|
| 233 |
+
except stripe.error.AuthenticationError:
|
| 234 |
+
view = DatePickerView()
|
| 235 |
+
await interaction.followup.send(
|
| 236 |
+
"β Unable to verify invoice. Please select your purchase date manually:",
|
| 237 |
+
view=view,
|
| 238 |
+
ephemeral=True,
|
| 239 |
+
)
|
| 240 |
+
return
|
| 241 |
+
|
| 242 |
+
purchase_date = datetime.fromtimestamp(inv.created)
|
| 243 |
+
purchase_date = purchase_date.replace(hour=0, minute=0, second=0, microsecond=0)
|
| 244 |
+
await interaction.followup.send(
|
| 245 |
+
format_eta_result(purchase_date),
|
| 246 |
+
ephemeral=True,
|
| 247 |
+
)
|
| 248 |
+
else:
|
| 249 |
+
view = DatePickerView()
|
| 250 |
+
await interaction.response.send_message(
|
| 251 |
+
"π€ Select your Reachy Mini purchase date:",
|
| 252 |
+
view=view,
|
| 253 |
+
ephemeral=True,
|
| 254 |
+
)
|
| 255 |
|
| 256 |
|
| 257 |
def run_bot():
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
discord.py
|
| 2 |
-
gradio
|
|
|
|
|
|
| 1 |
discord.py
|
| 2 |
+
gradio
|
| 3 |
+
stripe
|