Beispiel #1
0
    return Response::error("illegal request");
}
$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById($_SESSION['user']);
if (!$user) {
    return Response::error("user does not exist");
}
$th = TelegramCliHelper::getInstance();
$t = new TelegramCliWrapper($th->getSocket(), $th->isDebug());
$messages = $t->getHistory($user->phone, 1);
if (count($messages) < 1) {
    return Response::error('no messages received');
}
$message = $messages[0];
if (intval($message->unread) != 1) {
    return Response::error("no unread messages");
}
$text = strtolower(trim($message->text));
$response = "";
switch ($text) {
    case "help":
        $response = "These are the things you can ask me:\n" . "help => this info\n" . "remove me => remove my phone number from the system\n" . "send me a photo => invite system to send a photo\n" . "tell me a joke => I send to you something funny\n" . "say me the time => I send to you the current time on my timezone\n" . "weather => I send to you the weather where I live\n";
        break;
    case "weather":
        $weather = new OpenWeatherApi();
        $response = $weather->getWeatherInfoAsString();
        break;
    case "say me the time":
        $response = sprintf("The current time here is %s", date("l, F jS Y h:ia"));
        break;
    case "tell me a joke":
use TelegramCliWrapper\Models\User;
if (!isset($_POST['phone'])) {
    return Response::error('phone parameter missed');
}
$phone = trim($_POST['phone']);
if (!preg_match("/^\\d{9,15}\$/", $phone)) {
    return Response::error('phone parameter does not seems a phone number');
}
$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById($phone);
if ($user) {
    return Response::error('phone exists already in this system');
}
$phoneRequested = isset($_SESSION['phone_requested']) ? json_decode($_SESSION['phone_requested'], true) : array();
if (isset($phoneRequested[$phone]) && intval(date("U")) < $phoneRequested[$phone]) {
    return Response::error('phone already requested, you must to wait 15 minutes');
}
$phoneRequested[$phone] = intval(date("U")) + 15 * 60 * 60;
$_SESSION['phone_requested'] = json_encode($phoneRequested);
$th = TelegramCliHelper::getInstance();
$t = new TelegramCliWrapper($th->getSocket(), $th->isDebug());
$user = User::createUser($phone, $phone, "");
$t->addContact($user);
// send message with code in order to validate phone
$r = uniqid();
$user->code = substr($r, rand(1, strlen($r) - 6), 6);
$userStorage->save($user);
$msg = <<<EOD
Welcome to the telegram-cli-wrapper proof of concept
This is the code to access system, please keep it secret.
Code: {$user->code}
<?php

session_start();
include_once __DIR__ . '/../vendor/autoload.php';
use TelegramCliWrapper\TelegramCliWrapper;
use TelegramCliWrapper\TelegramCliHelper;
use TelegramCliWrapper\Storage\LocalFilesStorage;
use TelegramCliWrapper\Response;
use TelegramCliWrapper\Models\User;
if (!isset($_SESSION['user'])) {
    return Response::error("illegal request");
}
$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById($_SESSION['user']);
if (!$user) {
    return Response::error("user does not exist");
}
$th = TelegramCliHelper::getInstance();
$t = new TelegramCliWrapper($th->getSocket(), $th->isDebug());
$messages = $t->getHistory($user->phone, 100);
$result = array();
foreach ($messages as $message) {
    $result[] = array('text' => nl2br($message->text), 'from' => $message->from->phone, 'to' => $message->to->phone, 'date' => date("y.m.d H:j:s", $message->date));
}
return Response::ok(array('messages' => $result));
Beispiel #4
0
<?php

session_start();
include_once __DIR__ . '/../vendor/autoload.php';
use TelegramCliWrapper\TelegramCliWrapper;
use TelegramCliWrapper\TelegramCliHelper;
use TelegramCliWrapper\Storage\LocalFilesStorage;
use TelegramCliWrapper\Response;
use TelegramCliWrapper\Models\User;
if (!isset($_SESSION['user'])) {
    return Response::error("illegal request");
}
$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById($_SESSION['user']);
if (!$user) {
    return Response::error("user does not exist");
}
if (!isset($_POST['text'])) {
    return Response::error("text parameter missing");
}
$th = TelegramCliHelper::getInstance();
$t = new TelegramCliWrapper($th->getSocket(), $th->isDebug());
$t->msg($user->phone, $_POST['text']);
return Response::ok();
Beispiel #5
0
<?php

session_start();
include_once __DIR__ . '/../vendor/autoload.php';
use TelegramCliWrapper\Storage\LocalFilesStorage;
use TelegramCliWrapper\Response;
if (!isset($_POST['phone'])) {
    return Response::error('phone parameter missed');
}
if (!isset($_POST['code'])) {
    return Response::error('code parameter missed');
}
$code = trim($_POST['code']);
$phone = trim($_POST['phone']);
if (!preg_match("/^\\d{9,15}\$/", $phone)) {
    return Response::error('phone parameter does not seems a phone number');
}
$userStorage = new LocalFilesStorage('user');
$user = $userStorage->getById($phone);
if (!$user) {
    return Response::error('phone does not exist in this system');
}
if ($user->code != $code) {
    return Response::error('phone or code are wrong');
}
$user->logged = true;
$userStorage->save($user);
$_SESSION['user'] = $phone;
return Response::ok();