Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$start_text keeps getting send (update) #2

Open
puertajulian opened this issue Jul 10, 2015 · 6 comments
Open

$start_text keeps getting send (update) #2

puertajulian opened this issue Jul 10, 2015 · 6 comments

Comments

@puertajulian
Copy link

Ok, now I think what the problem is, my php.log shows the following:

[Fri Jul 10 10:25:23 2015] [error] [client 12x.9.6.12x] PHP Fatal error: Class 'Memcached' not found in /var/lib/openshift/user/app-root/runtime/repo/bot.php on line 115
14x.15x.16x.20x - - [10/Jul/2015:10:25:22 -0400] "POST /bot.php?sender=telegram HTTP/1.1" 500 - "-" "-"

line 115 for my file is:
$m = new Memcached();

Any clues? Im running this on OpenShift

@schmidtflo
Copy link
Owner

You need memcached for this script.

@puertajulian
Copy link
Author

I do have memcached installed and configured.

@schmidtflo
Copy link
Owner

Can you paste your full config file (obviously without the API keys..)?

@puertajulian
Copy link
Author

<?php
/* Configuration */

// Your Telegram-API-Key
$api_key = 'xxx';

// import.io Key
$import_io = 'c5df7ca8-8529-47e3-81e2-7bbeb9963955';
// The import.io fields for title and link
$import_io_title = 'entrytitle_link';
$import_io_link = 'modulethumb_link';

// Your memcached-credentials
$memcached_host = '127.9.6.131';
$memcached_port = 11211; // 0 if you use sockets, normally 11211 if you access it via port

// Your database
$db_host = 'mysql:host=127.9.6.130:3306;dbname=telegram';
$db_table = 'telegram';
$db_user = 'xxx';
$db_pw = 'xxx';

// Logging
$logging = true;

// Your telegram user_id (you can get it with logging)
$my_user_id = xxx;

// personal authentification key. Randomly created by you.
$auth_key = 'xxx';

$start_text = "Gracias por suscribirte!";
$stop_text = "Ok, se a removido la suscripcion";
$contact_text = "Puede contactarnos en: @puerta";
$help_text = "Utilice /suscribirse o /salir";
$latest_article_text = "Ultimas noticias: ";
$new_article_text = "Nueva noticia: ";

// Do not edit from here!
function sendmessage($type, $user, $content)
{
    global $api_key;
    $apiendpoint = ucfirst($type);

    if ($type == 'photo' || $type == "audio" || $type == "video" || $type == "document") {
        $mimetype = mime_content_type($content);
        $content = new CurlFile($content, $mimetype);
    } elseif ($type == "message") {
        $type = 'text';
    }


    $ch = curl_init("https://api.telegram.org/bot".$api_key."/send".$apiendpoint);
    curl_setopt_array($ch, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HEADER => false,
        CURLOPT_HTTPHEADER => array(
            'Host: api.telegram.org',
            'Content-Type: multipart/form-data'
        ),
        CURLOPT_POSTFIELDS => array(
            'chat_id' => $user,
            $type => $content
        ),
        CURLOPT_TIMEOUT => 0,
        CURLOPT_CONNECTTIMEOUT => 6000,
        CURLOPT_SSL_VERIFYPEER => false
    ));
    curl_exec($ch);
    curl_close($ch);
}


function updatecache()
{
    global $import_io, $import_io_title, $import_io_link, $memcached_host, $memcached_port;
    $articles = json_decode(file_get_contents($import_io));

    $current_article = $articles->results[0];
    $output = $current_article->{$import_io_title};
    $output .= " ";
    $output .= $current_article->{$import_io_link};

    $m = new Memcached();
    $m->addServer($memcached_host, $memcached_port);
    $m->set('latest_article', $output);
}


if (isset($_GET["sender"])) {
    if ($_GET["sender"] == "telegram") {
        $message = file_get_contents('php://input');
        $message = json_decode($message, true);

        if ($logging == true) {
            $statistik = date("d.m.Y H:i:s");
            $statistik .= " " . $message["message"]["text"] . "  //  " . $message["message"]["from"]["username"] . "\n";
            file_put_contents("log.txt", $statistik, FILE_APPEND);
        }

        $command = explode(" ", $message["message"]["text"]);
        $command = $command[0];

        if ($command == "/start") {

            sendmessage('message', $message["message"]["from"]["id"], $start_text);


            $db = new PDO($db_host, $db_user, $db_pw);
            $statement = "INSERT INTO {$db_table} (user_id) values ('" . $message["message"]["from"]["id"] . "')";
            $db->exec($statement);


            $m = new Memcached();
            $m->addServer($memcached_host, $memcached_port);
//            $m->delete('latest_article');

            $latest_article = $m->get('latest_article');
            if ($latest_article=='') {
                updatecache();
            }
            $text = $latest_article_text . $m->get('latest_article');
            sendmessage('message', $message["message"]["from"]["id"], $text);



            if ($logging) {
                $statement = "SELECT * FROM {$db_table}";
                $result = $db->query($statement);
                $result = $result->fetchAll();
                $count = count($result);
                sendmessage('message', $my_user_id, "Subscriptions: " . $count);
            }

        }

        if ($command == "/stop") {
            sendmessage('message', $message["message"]["from"]["id"], $stop_text);

            $db = new PDO($db_host, $db_user, $db_pw);
            $statement = "DELETE FROM {$db_table} where user_id = '" . $message["message"]["from"]["id"] . "'";
            $db->exec($statement);

            if ($logging) {
                $statement = "SELECT * FROM {$db_table}";
                $result = $db->query($statement);
                $result = $result->fetchAll();
                $count = count($result);
                sendmessage('message', $my_user_id, "Subscriptions: " . $count);
            }
        }


        if ($command == "/contact") {
            sendmessage('message', $message["message"]["from"]["id"], $contact_text);
        }


        if ($command == "/help") {
            sendmessage('message', $message["message"]["from"]["id"], $help_text);
        }

    }
}


if (isset($_GET["auth_key"])) {
    if ($_GET["auth_key"] == $auth_key) {


        $db = new PDO($db_host, $db_user, $db_pw);
        $statement = "SELECT * FROM ".$db_table;
        $user = $db->query($statement);
        $user = $user->fetchAll();

        $i = 0;
        if (isset($_GET["refresh"])) {
            if ($_GET["refresh"] == true) {
                updatecache();


                foreach ($user as $user_id) {
                    $i++;
                    $m = new Memcached();
                    $m->addServer($memcached_host, $memcached_port);
                    $text = $new_article_text . $m->get('latest_article');
                    sendmessage('message', $user_id[0], $text);
                }
            }
        }

        if (isset($_GET["message"])) {
            foreach ($user as $user_id) {
                $i++;
                sendmessage('message', $user_id[0], $_GET["message"]);
            }
        }
        echo 'Sent to '.$i.' subscribers.';

    }
}

?>

@schmidtflo
Copy link
Owner

Looks like the memcached-server is hosted on another IP (also the mysql).
You should write a short test-script, testing only the memcached-credentials.

@puertajulian
Copy link
Author

Looks like my memcached is installed but the server is refusing to connect. I also tried Memcached Cloud (https://redislabs.com/memcached-cloud) but no working either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants