-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter2telegram_bot.php
49 lines (41 loc) · 1.31 KB
/
twitter2telegram_bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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());
}
?>