Пример #1
0
 /**
  * @inheritdoc
  */
 public function handle($arguments)
 {
     $limit = 30;
     $update = $this->getUpdate();
     $weightLog = new WeightLog(Db::getInstance());
     $person = $weightLog->getPersonFromUpdate($update);
     $chartWeights = [];
     $weights = array_slice($weightLog->getAllWeightsByToken($person['token']), 0 - $limit);
     $chxl = [];
     $output = "Last {$limit} logs\nDate - Weight\n-----------------\n";
     foreach ($weights as $index => $weight) {
         $chxl[] = $index % 5 === 0 ? date('d/m', $weight['timestamp']) : '';
         $chartWeights[] = $weight['weight'];
         $output .= date('jS \\of M', $weight['timestamp']) . ' - ' . $weight['weight'] . 'lbs/kgs' . "\n";
     }
     $chartFile = sys_get_temp_dir() . '/weightlog-chart-' . $person['id'] . '.png';
     $min = min($chartWeights) - 5;
     $max = max($chartWeights) + 5;
     $query = ['chxt' => 'y,x', 'chxl' => '1:|' . implode('|', $chxl) . '|', 'chs' => '900x280', 'chxr' => "0,{$min},{$max}", 'chds' => "{$min},{$max}", 'cht' => 'lc', 'chco' => '0077CC', 'chd' => 't:' . implode(',', $chartWeights)];
     $url = "https://chart.googleapis.com/chart?" . http_build_query($query);
     file_put_contents($chartFile, file_get_contents($url));
     $this->replyWithMessage($output);
     if (file_exists($chartFile)) {
         $this->getTelegram()->sendPhoto($update['message']['chat']['id'], $chartFile, 'Your Weight');
         unlink($chartFile);
     }
     /*
     // This will update the chat status to typing...
     $this->replyWithChatAction(Actions::TYPING);
     
     // This will prepare a list of available commands and send the user.
     // First, Get an array of all registered commands
     // They'll be in 'command-name' => 'Command Handler Class' format.
     $commands = $this->getTelegram()->getCommands();
     
     // Build the list
     $response = '';
     foreach ($commands as $name => $command) {
         $response .= sprintf('/%s - %s' . PHP_EOL, $name, $command->getDescription());
     }
     
     // Reply with the commands list
     $this->replyWithMessage($response);
     
     // Trigger another command dynamically from within this command
     // When you want to chain multiple commands within one or process the request further.
     // The method supports second parameter arguments which you can optionally pass, By default
     // it'll pass the same arguments that are received for this command originally.
     $this->triggerCommand('subscribe');
     */
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function handle($arguments)
 {
     $weightLog = new WeightLog(Db::getInstance());
     $person = $weightLog->getPersonFromUpdate($this->getUpdate());
     $this->replyWithMessage("http://weightlog.ashleyhindle.com/?token={$person['token']}");
 }
Пример #3
0
<?php

$token = getenv('TELEGRAM_BOT_TOKEN');
if (empty($token)) {
    die('No token');
}
require 'src/bootstrap.php';
use Telegram\Bot\Api;
use WeightLog\WeightLog;
use WeightLog\Db;
$db = Db::getInstance();
$telegram = new Api($token);
$weightLog = new WeightLog($db);
$telegram->addCommand(\WeightLog\TelegramCommands\OutputCommand::class);
$telegram->addCommand(\WeightLog\TelegramCommands\ApiCommand::class);
$telegram->addCommand(\WeightLog\TelegramCommands\HelpCommand::class);
$run = true;
$lastUpdateId = 0;
while ($run) {
    sleep(1);
    $updates = $telegram->commandsHandler(false);
    if (empty($updates)) {
        continue;
    }
    foreach ($updates as $update) {
        $lastUpdateId = $update['update_id'];
        if (substr(trim($update['message']['text']), 0, 1) == '/') {
            continue;
        }
        $weightGiven = preg_replace('/[^0-9\\.]/', '', $update['message']['text']);
        $person = $weightLog->getPersonFromUpdate($update);
Пример #4
0
<?php

$token = isset($_GET['token']) ? $_GET['token'] : '';
if (empty($token)) {
    // Please ignore this terrbile code for now
    readfile('index.html');
    exit;
}
header('Content-Type: application/json');
require '../src/bootstrap.php';
use WeightLog\WeightLog;
use WeightLog\Db;
$db = Db::getInstance();
$weightLog = new WeightLog($db);
die(json_encode($weightLog->getAllWeightsByToken($token)));