├── README.md ├── LICENSE └── bot.php /README.md: -------------------------------------------------------------------------------- 1 | # inline-like-bot 2 | Telegram Inline Like Bot PHP 3 | 4 | 1) Upload bot.php and set webhook url 5 | 2) Add inline mode to your bot 6 | 3) Setup database 7 | 4) enjoying :) 8 | 9 | Demo : @inlinelike_bot 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ehsan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bot.php: -------------------------------------------------------------------------------- 1 | @inlinelike_bot 4 | * Author => Ehsan Noureddini 5 | * Email => me@ehsann.info 6 | */ 7 | define('API_KEY', API_KEY); 8 | define('WELCOME_MSG'," 9 | این ربات به شما این اجازه را می دهد که بتوانید پست هایی با دکمه لایک زیر آن ، ایجاد نمایید. 10 | ➖➖➖➖➖➖➖➖➖➖ 11 | ددر بخش ارسال پیام که هستید ، تایپ کنید @inlinelike_bot یه چیزیرا بنویسید و سپس روی ارسال کلیک کنید. 12 | برای مثال همینجا بنویسید: @inlinelike_bot من رو لایک کن 13 | "); 14 | function makeHTTPRequest($method,$datas=[]){ 15 | $url = "https://api.telegram.org/bot".API_KEY."/".$method; 16 | $ch = curl_init(); 17 | curl_setopt($ch,CURLOPT_URL,$url); 18 | curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 19 | curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($datas)); 20 | $res = curl_exec($ch); 21 | if(curl_error($ch)){ 22 | var_dump(curl_error($ch)); 23 | }else{ 24 | return json_decode($res); 25 | } 26 | } 27 | $data = file_get_contents("php://input"); 28 | $update = json_decode($data, true); 29 | $dsn = 'mysql:dbname=DB_NAME;host=localhost;charset=utf8'; 30 | $user = DB_USER; 31 | $password = DB_PASS; 32 | $pdo = new PDO($dsn, $user, $password); 33 | if($update['message']['text']=="/start"){ 34 | $chat_id=$update['message']['from']['id']; 35 | 36 | makeHTTPRequest('sendMessage',[ 37 | 'chat_id'=>$update['message']['from']['id'], 38 | 'text'=>WELCOME_MSG, 39 | 'parse_mode'=>'HTML', 40 | ]); 41 | } 42 | if(isset($update['inline_query'])){ 43 | $chat_id = $update['inline_query']['from']['id']; 44 | makeHTTPRequest('sendMessage',[ 45 | 'chat_id'=>"@testt12", 46 | 'text'=>json_encode($update), 47 | 'parse_mode'=>'HTML', 48 | ]); 49 | $inlineQueryID = $update['inline_query']['id']; 50 | makeHTTPRequest('answerInlineQuery',[ 51 | 'inline_query_id'=>$inlineQueryID, 52 | 'results' => json_encode([[ 53 | 'type' => 'article', 54 | 'id' => base64_encode(1), 55 | 'title' => 'Send?', 56 | 'input_message_content' => ['parse_mode' => 'HTML', 'message_text' => $update['inline_query']['query']], 57 | 'reply_markup' => [ 58 | 'inline_keyboard'=>[ 59 | [ 60 | ['text'=> "❤",'callback_data'=>'like'] 61 | ] 62 | ]] 63 | ]]) 64 | ]); 65 | } 66 | 67 | if(isset($update['callback_query']) && $update['callback_query']['data']=="like"){ 68 | $alert = "شما این رو ❤ دارید :)"; 69 | $callBackQueryID = $update['callback_query']['id']; 70 | $callBackQueryChatID = $update['callback_query']['message']['chat']['id']; 71 | if($callBackQueryChatID==""){ //inline callbackquery 72 | $callBackQueryChatID=$update['callback_query']['from']['id']; 73 | } 74 | $callBackQueryMessageID = $update['callback_query']['message']['message_id']; 75 | if($callBackQueryMessageID==""){ 76 | $callBackQueryMessageID=$update['callback_query']['inline_message_id']; 77 | } 78 | 79 | $userID = $update['callback_query']['from']['id']; 80 | $firstName = $update['callback_query']['from']['first_name']; 81 | $lastName = $update['callback_query']['from']['last_name']; 82 | $userName = $update['callback_query']['from']['username']; 83 | 84 | 85 | // insert like 86 | $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); 87 | $st = $pdo->prepare("INSERT INTO likes(`chat_id`,`query_id`,`message_id`,`user_id`) VALUES(:chat_id,:query_id,:message_id,:user_id)"); 88 | $st->bindParam(":chat_id",$callBackQueryChatID); 89 | $st->bindParam(":query_id",$callBackQueryID); 90 | $st->bindParam(":message_id", $callBackQueryMessageID); 91 | $st->bindParam(":user_id", $userID); 92 | $exec = $st->execute(); 93 | if(!$exec){ 94 | $alert = "You already liked this post"; 95 | } 96 | else{ 97 | //select likes 98 | $st= $pdo->prepare("select count(*) as like_count from likes where message_id=:message_id"); 99 | $st->bindParam(":message_id", $callBackQueryMessageID); 100 | $st->execute(); 101 | $like_count = $st->fetch()['like_count']+1; // +1 for hearts 102 | 103 | if(intval($like_count)<10){ 104 | $likes = str_repeat('❤',$like_count); 105 | } 106 | else{ 107 | $likes = $like_count . ' ' . '❤'; 108 | } 109 | makeHTTPRequest("editMessageReplyMarkup",[ 110 | 'inline_message_id'=>$callBackQueryMessageID, 111 | 'reply_markup' => json_encode([ 112 | 'inline_keyboard'=>[ 113 | [ 114 | // ['text'=> $like_count . ' ❤','callback_data'=>'like'] 115 | ['text'=> $likes,'callback_data'=>'like'] 116 | ] 117 | ]]), 118 | ]); 119 | } 120 | makeHTTPRequest("answerCallbackQuery", [ 121 | 'callback_query_id' => $callBackQueryID, 122 | 'text' => $alert, 123 | // 'show_alert' => true, 124 | ] ); 125 | 126 | } 127 | 128 | 129 | 130 | ?> 131 | --------------------------------------------------------------------------------