> ## Documentation Index
> Fetch the complete documentation index at: https://help.bots.lt/llms.txt
> Use this file to discover all available pages before exploring further.

# Global Libraries (Libs)

> Built-in libraries for random generation, date/time, virtual resources, cryptocurrency prices, and webhooks.

Bots.LT injects a powerful set of built-in libraries globally under the `Libs` object. **No imports needed.**

***

## Libs.Random

Generate random values for referral codes, tokens, dice rolls, and more.

### randomInt

Generates a random integer between `min` and `max` (inclusive).

| Parameter | Type    | Required | Description                |
| --------- | ------- | -------- | -------------------------- |
| `min`     | Integer | Yes      | Minimum value (inclusive). |
| `max`     | Integer | Yes      | Maximum value (inclusive). |

```python BLP Example theme={null}
# Roll a dice
roll = Libs.Random.randomInt(1, 6)
Bot.sendMessage(chat_id=u, text=f"You rolled: {roll} 🎲")

# Random reward amount between 10 and 100
reward = Libs.Random.randomInt(10, 100)
Bot.sendMessage(chat_id=u, text=f"You won {reward} coins!")
```

***

### randomStr

Generates a random alphanumeric string.

| Parameter | Type    | Required | Description                       |
| --------- | ------- | -------- | --------------------------------- |
| `length`  | Integer | Yes      | Length of the string to generate. |

```python BLP Example theme={null}
# Generate a referral code
ref_code = Libs.Random.randomStr(8)
User.saveData("referral_code", ref_code)
Bot.sendMessage(chat_id=u, text=f"Your referral code: {ref_code}")

# Generate a temporary password
temp_pass = Libs.Random.randomStr(12)
```

***

### randomFloat

Generates a random floating-point number between `0.0` and `1.0`.

| Parameter | Type | Required | Description    |
| --------- | ---- | -------- | -------------- |
| *(none)*  | —    | —        | No parameters. |

```python BLP Example theme={null}
chance = Libs.Random.randomFloat()
if chance > 0.5:
    Bot.sendMessage(chat_id=u, text="You won the coin flip! ✅")
else:
    Bot.sendMessage(chat_id=u, text="Better luck next time! ❌")
```

***

### randomAscii

Generates a random string using all ASCII letters (including uppercase).

| Parameter | Type    | Required | Description           |
| --------- | ------- | -------- | --------------------- |
| `length`  | Integer | Yes      | Length of the string. |

```python BLP Example theme={null}
secret_key = Libs.Random.randomAscii(16)
Bot.saveData("api_secret", secret_key)
```

***

## Libs.DateAndTime

Get current date and time in any timezone.

> **Note:** `time.sleep()` and standard Python sleep methods are **blocked** on the BLP platform. Use `Bot.runCommandAfter(seconds, command)` for delays.

### now

Returns a detailed time object for a specific timezone.

| Parameter      | Type   | Required | Description                                                                              |
| -------------- | ------ | -------- | ---------------------------------------------------------------------------------------- |
| `timezone_str` | String | No       | Timezone name (e.g., `"Asia/Dhaka"`, `"America/New_York"`). Defaults to server timezone. |

**Returns** a dictionary with keys: `date`, `time`, `datetime`, `timestamp`, `year`, `month`, `day`, `hour`, `minute`, `second`, `weekday`.

```python BLP Example theme={null}
now = Libs.DateAndTime.now("Asia/Dhaka")
Bot.sendMessage(
    chat_id=u,
    text=f"📅 Date: {now['date']}\n🕐 Time: {now['time']}"
)

# Use timestamp for expiry logic
ts = now['timestamp']
User.saveData("last_active", ts)
```

***

### utcnow

Returns the current UTC time as a dictionary.

| Parameter | Type | Required | Description    |
| --------- | ---- | -------- | -------------- |
| *(none)*  | —    | —        | No parameters. |

```python BLP Example theme={null}
utc = Libs.DateAndTime.utcnow()
Bot.sendMessage(chat_id=u, text=f"UTC Time: {utc['time']}")
```

***

### date\_now

Returns only the current date as a formatted string `YYYY-MM-DD`.

| Parameter | Type | Required | Description    |
| --------- | ---- | -------- | -------------- |
| *(none)*  | —    | —        | No parameters. |

```python BLP Example theme={null}
today = Libs.DateAndTime.date_now()
User.saveData("joined_date", today)
Bot.sendMessage(chat_id=u, text=f"Your join date: {today}")
```

***

### time (UNIX Timestamp)

Returns the current UNIX timestamp (seconds since epoch) as an integer.

| Parameter | Type | Required | Description    |
| --------- | ---- | -------- | -------------- |
| *(none)*  | —    | —        | No parameters. |

```python BLP Example theme={null}
current_ts = Libs.DateAndTime.time()

# Check if user's subscription is still valid
expiry = User.getData("subscription_expiry")
if expiry and current_ts < int(expiry):
    Bot.sendMessage(chat_id=u, text="✅ Subscription active!")
else:
    Bot.sendMessage(chat_id=u, text="❌ Subscription expired.")
```

***

## Libs.Resources

Manage virtual balances, point systems, and leaderboards — without writing any database queries.

### Accessing Resources

| Method                                     | Description                                      |
| ------------------------------------------ | ------------------------------------------------ |
| `Libs.Resources.userRes(name)`             | Resource for the **current user**.               |
| `Libs.Resources.anotherRes(name, user_id)` | Resource for a **specific user** (by `user_id`). |
| `Libs.Resources.globalRes(name)`           | Shared resource for the **entire bot**.          |

### Resource Methods

Once accessed, each resource object supports:

| Method         | Description                                |
| -------------- | ------------------------------------------ |
| `.value()`     | Returns current value as Integer or Float. |
| `.add(amount)` | Adds `amount` to the resource.             |
| `.cut(amount)` | Subtracts `amount` from the resource.      |
| `.set(amount)` | Sets resource to an exact value.           |
| `.reset()`     | Resets value to `0`.                       |

```python BLP Example theme={null}
# Get current user's coins
coins = Libs.Resources.userRes("coins")

current = coins.value()
Bot.sendMessage(chat_id=u, text=f"💰 You have {current} coins")

# Reward coins
coins.add(50)

# Deduct coins
if coins.value() >= 10:
    coins.cut(10)
    Bot.sendMessage(chat_id=u, text="You spent 10 coins!")
else:
    Bot.sendMessage(chat_id=u, text="Not enough coins!")
```

```python BLP Example (Leaderboard) theme={null}
# Get the top 10 users by "points"
top = Libs.Resources.getTop("points", limit=10)
lines = [f"{i+1}. User {r['user_id']}: {r['value']} pts" for i, r in enumerate(top)]
Bot.sendMessage(chat_id=u, text="\n".join(lines))
```

```python BLP Example (Shared Global Counter) theme={null}
# Increment a global visits counter
visits = Libs.Resources.globalRes("total_visits")
visits.add(1)
Bot.sendMessage(chat_id=u, text=f"Total bot visits: {visits.value()}")
```

***

## Libs.Crypto

Fetch real-time cryptocurrency prices with automatic fallbacks across multiple APIs (Coinbase, Binance, KuCoin).

### get\_price

Gets the current price of a cryptocurrency.

| Parameter   | Type   | Required | Description                                    |
| ----------- | ------ | -------- | ---------------------------------------------- |
| `from_coin` | String | Yes      | Coin symbol (e.g., `"BTC"`, `"ETH"`, `"TRX"`). |
| `to_coin`   | String | No       | Target currency. Default: `"USD"`.             |

```python BLP Example theme={null}
btc = Libs.Crypto.get_price("BTC", "USD")
Bot.sendMessage(chat_id=u, text=f"₿ Bitcoin: ${btc:,.2f}")
```

***

### convert

Converts an amount from one currency to another.

| Parameter   | Type   | Required | Description         |
| ----------- | ------ | -------- | ------------------- |
| `from_coin` | String | Yes      | Source coin symbol. |
| `to_coin`   | String | Yes      | Target currency.    |
| `amount`    | Float  | Yes      | Amount to convert.  |

```python BLP Example theme={null}
# Convert 500 TRX to USD
usd = Libs.Crypto.convert("TRX", "USD", 500)
Bot.sendMessage(chat_id=u, text=f"500 TRX = ${usd:.2f}")

# Convert user's BTC balance to USD
btc_balance = float(User.getData("btc_balance") or 0)
usd_value = Libs.Crypto.convert("BTC", "USD", btc_balance)
Bot.sendMessage(chat_id=u, text=f"Your BTC wallet ≈ ${usd_value:.2f}")
```

***

### get\_coin\_info

Returns detailed 24-hour market data for a coin.

| Parameter | Type   | Required | Description                           |
| --------- | ------ | -------- | ------------------------------------- |
| `coin`    | String | Yes      | Coin symbol (e.g., `"BTC"`, `"ETH"`). |

**Returns** a dictionary with: `price`, `market_cap`, `volume_24h`, `change_24h` (%), `high_24h`, `low_24h`.

```python BLP Example theme={null}
info = Libs.Crypto.get_coin_info("ETH")
Bot.sendMessage(
    chat_id=u,
    text=f"""📊 ETH Market Stats
💰 Price: ${info['price']:,.2f}
📈 24h Change: {info['change_24h']:.2f}%
🔺 24h High: ${info['high_24h']:,.2f}
🔻 24h Low: ${info['low_24h']:,.2f}
📦 Volume: ${info['volume_24h']:,.0f}"""
)
```

***

## Libs.Webhook

Generate secure, encrypted webhook URLs that trigger specific bot commands when accessed via HTTP.

### getUrlFor

| Parameter   | Type    | Required | Description                                                  |
| ----------- | ------- | -------- | ------------------------------------------------------------ |
| `command`   | String  | Yes      | Command to trigger (e.g., `"/payment_success"`).             |
| `user_id`   | Integer | No       | User ID for the triggered context. Defaults to current user. |
| `chat_id`   | Integer | No       | Chat ID for the triggered context.                           |
| `**options` | Any     | No       | Any additional key-value data passed to the command context. |

```python BLP Example theme={null}
# Generate a payment webhook
checkout_url = Libs.Webhook.getUrlFor(
    "/payment_success",
    user_id=u,
    amount=99,
    plan="premium"
)

Bot.sendMessage(
    chat_id=u,
    text=f"Complete payment here:\n{checkout_url}"
)
```

```python BLP Example (Used with external payment gateway) theme={null}
# In /create_invoice
invoice_url = Libs.Webhook.getUrlFor("/on_payment", user_id=u, order_id="ORD-123")
# Pass invoice_url to your payment provider as the callback/IPN URL
# When the payment succeeds, the provider calls invoice_url
# which triggers /on_payment for this user

# In /on_payment command
order_id = options.get("order_id")
Bot.sendMessage(chat_id=u, text=f"✅ Payment received for order {order_id}!")
```
