In the digital era, integrating social media feeds into applications has become increasingly popular. This article demonstrates how to create a Telegram bot in PHP that fetches tweets based on hashtags. This can be a valuable tool for businesses, communities, or individuals who want to stay updated with specific topics on Twitter.
Getting Started
To begin, you need API keys for both Twitter and Telegram, and a PHP environment set up with necessary libraries.
Obtaining API Keys
- Twitter API Keys:
- Visit the Twitter Developer Platform.
- Sign up for a developer account if you don’t have one.
- Create a new application in your developer dashboard.
- Once the application is created, you will get
consumer_key
,consumer_secret
,access_token
, andaccess_token_secret
.
- Telegram Bot Token:
- Open Telegram and search for
@BotFather
. - Follow the instructions to create a new bot.
BotFather
will provide you with a token upon the creation of your bot.
- Open Telegram and search for
Setting Up the PHP Environment
You need to install two PHP libraries via Composer: abraham/twitteroauth
for Twitter API and longman/telegram-bot
for Telegram bot functionality.
composer require abraham/twitteroauth composer require longman/telegram-bot
PHP Code for the Telegram Bot
Here’s the PHP script to set up your Telegram bot for fetching tweets:
<?php require_once 'vendor/autoload.php'; use Abraham\TwitterOAuth\TwitterOAuth; use Longman\TelegramBot\Telegram; use Longman\TelegramBot\Request; // Twitter API credentials $consumerKey = '...'; $consumerSecret = '...'; $accessToken = '...'; $accessTokenSecret = '...'; // Telegram Bot Token $telegramToken = '...'; // Twitter OAuth $twitter = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret); // Telegram Bot $telegram = new Telegram($telegramToken); // Command handler function handleCommand($messageText, $chatId) { global $twitter; $tweets = $twitter->get("search/tweets", ["q" => $messageText, "count" => 5, "tweet_mode" => "extended"]); foreach ($tweets->statuses as $tweet) { $responseText = "Tweet from @" . $tweet->user->screen_name . ":\n" . $tweet->full_text; $response = [ 'chat_id' => $chatId, 'text' => $responseText ]; Request::sendMessage($response); } } // Process updates from Telegram try { $telegram->handle(); $update = json_decode(file_get_contents('php://input'), true); $chatId = $update['message']['chat']['id']; $messageText = $update['message']['text']; handleCommand($messageText, $chatId); } catch (Exception $e) { // Log exceptions error_log($e->getMessage()); } ?>
Here is my code on Github: https://github.com/nodesbond/phpTelegramBot-twitter/blob/main/twitter2telegram_bot.php
This PHP script provides a simple yet effective way to integrate Twitter feeds into your Telegram bot. By following the steps above, you can create a personalized experience for your users, keeping them informed about specific topics and discussions happening on Twitter.
If you’re interested in a similar solution but prefer Python, be sure to check out our analogous guide for Python. It offers a comprehensive walkthrough on creating a Twitter-integrated Telegram bot using Python, catering to those who are more familiar with or prefer Python as their programming language. The Python version provides the same functionality with a syntax and approach unique to Python programming. Click here to explore the Python guide.