Last active
May 30, 2017 23:49
-
-
Save Deifinger/52ccfafcfb019f31480d316c4ba439d6 to your computer and use it in GitHub Desktop.
First Telegram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"telegram-bot/api": "^2.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
define('BOT_TOKEN', 'TOKEN'); | |
define('BOT_USERNAME', 'BotName'); | |
$timestampFilename = './last_timestamp'; | |
$startMessage = 'С Днем Рождения, здоровяк! Пиши команду /get_task и получай задание'; | |
$questMessage = 'Поковыряйся пальцем в носу, там ты найдешь записку'; | |
$timestampFile = fopen($timestampFilename, 'r') or die('Unable to open file!'); | |
$lastTimestamp = fread($timestampFile, filesize($timestampFilename)); | |
fclose($timestampFile); | |
try { | |
$bot = new \TelegramBot\Api\BotApi(BOT_TOKEN); | |
$updates = $bot->getUpdates(); | |
$count = sizeof($updates); | |
for($i = 0; $i < $count; $i++) { | |
$message = $updates[$i]->getMessage(); | |
$chatId = $message->getChat()->getId(); | |
$text = $message->getText(); | |
$date = $message->getDate(); | |
if($lastTimestamp >= $date) continue; | |
if($text == '/start') { | |
$bot->sendMessage($chatId, $startMessage); | |
} else if($text == '/get_task') { | |
$bot->sendMessage($chatId, $questMessage); | |
} | |
} | |
// PHP specific code, because $date variable is not nulled after cycle | |
$timestampFile = fopen($timestampFilename, 'w'); | |
fwrite($timestampFile, $date); | |
fclose($timestampFile); | |
} catch (\TelegramBot\Api\Exception $e) { | |
echo $e->getMessage(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1496185858 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment