This is a tutorial for a Telegram bot that automatically fetches and sends stock price updates. I think this is a good starting project with a single API.

Key Components

PHP script

Telegram Bot API

Alpha Vantage API for stock data

SQLite database

Setting Up the Telegram Bot

Before diving into the code, you need to create a Telegram bot:

Open Telegram and search for “BotFather”

Start a chat with BotFather and use the /newbot command

Follow the prompts to name your bot and choose a username

BotFather will provide you with a token - keep this secure!

Implementation

Set up environment:

Install dependencies using Composer. Configure .env file with API keys. Create main script:

<?php
// bot.php
require 'config.php';
require 'functions.php';
require 'database.php';

$pdo = setupDatabase();

$symbols = ['SPY', 'TSLA', 'NVDA'];

foreach ($symbols as $symbol) {
    $price = getStockPrice($symbol);
    saveStockPrice($pdo, $symbol, $price);

Now let’s add the core functions:

<?php
// functions.php
require 'config.php';
require 'database.php';

function getStockPrice($symbol) {
    $url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={$symbol}&interval=5min&apikey=" . ALPHA_VANTAGE_API_KEY;
    $data = file_get_contents($url);
    $json = json_decode($data, true);
    $timeSeries = $json['Time Series (5min)'];
    $latest = reset($timeSeries);
    return $latest['4. close'];
}

function sendMessage($message) {
    $url = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN . "/sendMessage";
    $data = [
        'chat_id' => TELEGRAM_CHAT_ID,
        'text' => $message,
    ];
    file_get_contents($url . '?' . http_build_query($data));
}

Now even more functions:

<?php
// database.php
if (!function_exists('setupDatabase')) {
    function setupDatabase() {
        $pdo = new PDO('sqlite:' . DB_PATH);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec('PRAGMA journal_mode = WAL;');
        $pdo->exec('
            CREATE TABLE IF NOT EXISTS stock_prices (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                symbol TEXT NOT NULL,
                price REAL NOT NULL,
                fetched_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )
        ');
        return $pdo;
    }
}

if (!function_exists('saveStockPrice')) {
    function saveStockPrice($pdo, $symbol, $price) {
        $stmt = $pdo->prepare('INSERT INTO stock_prices (symbol, price) VALUES (:symbol, :price)');
        $stmt->execute(['symbol' => $symbol, 'price' => $price]);
    }
}

if (!function_exists('getLatestPrices')) {
    function getLatestPrices($pdo) {
        $stmt = $pdo->query('
            SELECT symbol, price, fetched_at 
            FROM stock_prices 
            WHERE fetched_at = (SELECT MAX(fetched_at) FROM stock_prices WHERE symbol = stock_prices.symbol)
        ');

Now everything is functionally functional because of the functions functioning.

Use cron job to run the bot script periodically, and now you have a fully automated system that fetches stock prices, stores them in a db, and sends daily summaries via Telegram. A next time is to add more features and make sure you compress your logs, but I’ll leave that up to you.

Bots leverage APIs and automation for an unbelievable amount of tasks, but now think about using AI to do something. What will you bot?