zelenin / telegram-bot-api

Telegram Bot API Client
MIT License
58 stars 21 forks source link

error server #9

Closed farbodmahini closed 8 years ago

farbodmahini commented 8 years ago

when i use your newest library , this code show me Error server, But when i use your oldest version , show "Code: 400. Description: "[Error]: Bad Request: chat_id is empty" and works fine ! please help me to work with your newes library !

Code : require 'vendor/autoload.php'; require 'lib/config.php' $client = new Zelenin\Telegram\Bot\Api($token); // Set your access token $update = json_decode(file_get_contents('php://input')); try { $test = 'dgd'; $response = $client->sendMessage([ 'chat_id' => $update->message->chat->id, 'text' => $test ]); } catch (\Zelenin\Telegram\Bot\NotOkException $e) { echo $e->getMessage(); }

zelenin commented 8 years ago

wrong usage. try daemon:

$client = new Api($config['token']);

$daemon = new \Zelenin\Telegram\Bot\Daemon\Daemon($client);

$daemon
    ->onUpdate(function (\Zelenin\Telegram\Bot\Type\Update $update) use ($memory) {
        echo $update->message->text . PHP_EOL;
        echo PHP_EOL;

       print_r($update);
    });

$daemon->run();
farbodmahini commented 8 years ago

i tested this too ! but it's not work !

<?php require 'vendor/autoload.php'; require 'lib/config.php'; $client = new Api($token); $daemon = new \Zelenin\Telegram\Bot\Daemon\Daemon($client); $daemon ->onUpdate(function (\Zelenin\Telegram\Bot\Type\Update $update) use ($memory) { echo $update->message->text . PHP_EOL; echo PHP_EOL; print_r($update); }); $daemon->run(); ?>

zelenin commented 8 years ago

i just test it and it works.

farbodmahini commented 8 years ago

i use webhook , "Daemon" support webhook or Update cron ?

zelenin commented 8 years ago

sure.

try var_dump($update); to see what you get from hook

farbodmahini commented 8 years ago

nothing ! it's the same error ! why i can't use the default usage with your newest version ?! with your older library i worked as well ! now i want to use the newest ! i don't know whats the problem ?!!

zelenin commented 8 years ago

well. You don't receive anything from telegram server. It's not about this library.

farbodmahini commented 8 years ago

this code : http://pastebin.com/gGLEBQAK it's work as well when i use the older library , and telegram send me the requests , my server receive that reqs ! when i see the webhook url , show me this : Code: 400. Description: "[Error]: Bad Request: chat_id is empty".====> it's good ! But when i changed the library ( or change the code with daemon ) then my server can't receive any requests from telegram and show me the 500 error server !

zelenin commented 8 years ago

you say that $jsondata = file_get_contents('php://input'); is empty. it's not about library. There is must be Update object from Telegram.

farbodmahini commented 8 years ago

when i browse the webhook $jsondata is empty , but then i send a message to my bot , telegram send a json data to my webhook and that time $jsondata isn't empty !

zelenin commented 8 years ago

just try

$client = new Zelenin\Telegram\Bot\Api($token); // Set your access token

try {
    $response = $client->sendMessage([
        'chat_id' => $chatId,
        'text' => 'test'
    ]);
} catch (Zelenin\Telegram\Bot\Exception\NotOkException $e) {
    echo $e->getMessage();
}

$chatId is your chat id. I just try this code, it works on last version (0.0.8)

farbodmahini commented 8 years ago

Thanks ! your code works , becuase of this = > my problem was this code : catch (\Zelenin\Telegram\Bot\NotOkException $e) i'v changed to catch (Zelenin\Telegram\Bot\Exception\NotOkException $e)

Your Usage Code Example in Readme is wronge ! your Usage in 'Readme' was with catch (\Zelenin\Telegram\Bot\NotOkException $e) for fix it , change it with catch (Zelenin\Telegram\Bot\Exception\NotOkException $e)

zelenin commented 8 years ago

Use IDE like PHPStorm.