<?php

/**
 * MeeChat to Telegram 消息同步
 * @author solu
 */
$index = (int) $argv[1];
require_once realpath(dirname(__FILE__)) . '/../common.php';

ini_set("display_errors", "On");
ini_set("default_socket_timeout", -1);
error_reporting(E_ALL & ~E_NOTICE);

$objRedis = dwRedis::init();
$objRedis->setOption(Redis::OPT_READ_TIMEOUT, -1);
for ($i = 0; $i < 100000; $i++) {
    _log("step: {$i}");
    $msg = Telegram::popMessageList($objRedis);

    $msg = json_decode($msg, true);
    if (!$msg['chat_id'] || !$msg['text']) {
        continue;
    }
    try {
        Telegram::apiRequest("sendMessage", $msg);
    } catch (Exception $e) {
        _log($e->getMessage());
        continue;
    }

    _log("sendMessage to chat_id:{$msg['chat_id']}");

    sleep(0.1);
}


function _log($msg) {
    $mem_usage = memory_get_usage(true);
    $time = date('Y-m-d H:i:s');
    echo "[{$time}][Mem:{$mem_usage}] {$msg}\n";
}

