badfarm / zanzara

Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
MIT License
201 stars 28 forks source link

Not working on Windows #2

Closed foremtehan closed 4 years ago

foremtehan commented 4 years ago

I did what you said in the doc but my bot doesnt response to me what is the problem ?

cheeghi commented 4 years ago

Hi, without further details it's difficult to say where is the problem, can you send us your code? Do you see any error message in console?

foremtehan commented 4 years ago

I did these:

  1. mkdir test
  2. cd test
  3. composer require badfarm/zanzara
  4. touch index.php
  5. inside index.php:
<?php

use Zanzara\Zanzara;
use Zanzara\Context;

require __DIR__ . '/vendor/autoload.php';

$bot = new Zanzara("TOKEN");

$bot->onCommand('onText', function (Context $ctx) {
    $ctx->sendMessage('Hello');
});

$bot->run();
  1. php index.php (i dont know why it closes itself after a while for about 20sec, even i do ini_set('max_execution_time', 0) it will close)
  2. I went to my bot chat and sending some text
  3. my bot doesnt response even on /start command
cheeghi commented 4 years ago

So, if you use $bot->onCommand('onText') the bot will reply only on command /onText. If you want the bot to reply to any message you can use the $bot->onMessage() method. For the problem of closing itself after 20s it's not necessary to set the max_execution_time to 0. When you run your script (php index.php) do you see the Zanzara is listening... message? Which version of PHP are you using?

foremtehan commented 4 years ago

you can use the $bot->onMessage()

Ok i change it but not working:

$bot->onMessage(function (Context $ctx) {
    $ctx->sendMessage('Hello');
});

do you see the Zanzara is listening... message?

No, I dont (it doenst on cmd either): image

Which version of PHP are you using?

PHP 7.4.1 (on XAMPP)

bohzio commented 4 years ago

Hi, it's a certification problem of xampp. To fix this problem you need to change some configuration of the php.ini file. You can try with this way:

foremtehan commented 4 years ago

@bohzio thanks but its not working i also stop/started the xampp

cheeghi commented 4 years ago

We have just released the 0.3.1 version, this one doesn't resolve the problem, but the error should now be logged in console, so we can troubleshoot it. Can you upgrade to this version and check if there's any error in console? Thank you

foremtehan commented 4 years ago

@cheeghi :

Failed to call Telegram Bot Api, reason: RuntimeException: Connection to api.telegram.org:443 failed during DNS lookup: DNS error in C:\Users\\Docum
ents\TelegramBots\test\vendor\react\socket\src\HappyEyeBallsConnectionBuilder.php:129
Stack trace:
#0 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(28): React\Socket\HappyEyeBallsConnectionBuilder->React\Socket
\{closure}(Object(RuntimeException))
#1 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(134): React\Promise\RejectedPromise->then(NULL, Object(Closure))
#2 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Prom
ise\RejectedPromise))
#3 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedPromi
se))
#4 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#5 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(42): React\Promise\Promise::React\Promise\{closure}(Object(Run
timeException))
#6 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(135): React\Promise\RejectedPromise->done(Object(Closure), Object(Clos
ure), Object(Closure))
#7 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Prom
ise\RejectedPromise))
#8 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedPromi
se))
#9 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#10 C:\Users\\Documents\TelegramBots\test\vendor\react\dns\src\Query\CachingExecutor.php(56): React\Promise\Promise::React\Promise\{closure}(Object(
RuntimeException))
#11 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(28): React\Dns\Query\CachingExecutor->React\Dns\Query\{closur
e}(Object(RuntimeException))
#12 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(134): React\Promise\RejectedPromise->then(Object(Closure), Object(Clo
sure))
#13 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#14 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#15 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#16 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(42): React\Promise\Promise::React\Promise\{closure}(Object(Ru
ntimeException))
#17 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(135): React\Promise\RejectedPromise->done(Object(Closure), Object(Clo
sure), Object(Closure))
#18 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#19 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#20 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#21 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(28): React\Promise\Promise::React\Promise\{closure}(Object(Ru
ntimeException))
#22 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(134): React\Promise\RejectedPromise->then(Object(Closure), Object(Clo
sure))
#23 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#24 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#25 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#26 C:\Users\\Documents\TelegramBots\test\vendor\react\dns\src\Query\SelectiveTransportExecutor.php(76): React\Promise\Promise::React\Promise\{closu
re}(Object(RuntimeException))
#27 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(28): React\Dns\Query\SelectiveTransportExecutor->React\Dns\Qu
ery\{closure}(Object(RuntimeException))
#28 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(134): React\Promise\RejectedPromise->then(Object(Closure), Object(Clo
sure))
#29 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#30 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#31 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(RuntimeException))
#32 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Deferred.php(43): React\Promise\Promise::React\Promise\{closure}(Object(RuntimeEx
ception))
#33 C:\Users\\Documents\TelegramBots\test\vendor\react\dns\src\Query\RetryExecutor.php(45): React\Promise\Deferred->reject(Object(RuntimeException))
#34 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(28): React\Dns\Query\RetryExecutor->React\Dns\Query\{closure}
(Object(RuntimeException))
#35 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(134): React\Promise\RejectedPromise->then(Object(Closure), Object(Clo
sure))
#36 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#37 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#38 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(React\Dns\Query\TimeoutExc
eption))
#39 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\RejectedPromise.php(42): React\Promise\Promise::React\Promise\{closure}(Object(Re
act\Dns\Query\TimeoutException))
#40 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(135): React\Promise\RejectedPromise->done(Object(Closure), Object(Clo
sure), Object(Closure))
#41 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(168): React\Promise\Promise::React\Promise\{closure}(Object(React\Pro
mise\RejectedPromise))
#42 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(148): React\Promise\Promise->settle(Object(React\Promise\RejectedProm
ise))
#43 C:\Users\\Documents\TelegramBots\test\vendor\react\promise\src\Promise.php(237): React\Promise\Promise->reject(Object(React\Promise\Timer\Timeou
tException))
#44 C:\Users\\Documents\TelegramBots\test\vendor\react\promise-timer\src\functions.php(47): React\Promise\Promise::React\Promise\{closure}(Object(Re
act\Promise\Timer\TimeoutException))
#45 C:\Users\\Documents\TelegramBots\test\vendor\react\event-loop\src\Timer\Timers.php(96): React\Promise\Timer\{closure}(Object(React\EventLoop\Tim
er\Timer))
#46 C:\Users\\Documents\TelegramBots\test\vendor\react\event-loop\src\StreamSelectLoop.php(184): React\EventLoop\Timer\Timers->tick()
#47 C:\Users\\Documents\TelegramBots\test\vendor\badfarm\zanzara\src\Zanzara\Zanzara.php(160): React\EventLoop\StreamSelectLoop->run()
#48 C:\Users\\Documents\TelegramBots\test\index.php(14): Zanzara\Zanzara->run()
#49 {main}
bohzio commented 4 years ago

could you run this curl request? (be sure to keep "bot" in front of the token) curl https://api.telegram.org/botYOUR-BOT-TOKEN/getMe

foremtehan commented 4 years ago

Yes it returns a json with my bot information, One thing, In my country telegram is blocked, But i'm behind a vpn , I can make a request via browser and git bash, ngrok, etc.., i can see the api.telegram.org:443 in the browser it redirect me to https://core.telegram.org/bots

bohzio commented 4 years ago

Ok, could you try to manually set a dns server that works in your country?

<?php

use React\Socket\Connector;
use Zanzara\Zanzara;
use Zanzara\Context;
use Zanzara\Config;

require __DIR__ . '/vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$connector = new Connector($loop, array(
    'timeout' => 3.0,
    'dns' => '8.8.8.8'
));

$config = new Config();
$config->setConnector($connector);
$config->setLoop($loop);

$bot = new Zanzara("TOKEN", $config);

$bot->onCommand('start', function (Context $ctx) {
    $ctx->sendMessage('Hello');
});

$bot->run();
foremtehan commented 4 years ago

Now this is working thanks you very much.