Eleirbag89 / TelegramBotPHP

A very simple PHP Telegram Bot API for sending messages.
http://eleirbag89.github.io/TelegramBotPHP
MIT License
800 stars 340 forks source link

Moved site to another server. Do I need to recompose? #219

Closed ScotterMonk closed 3 years ago

ScotterMonk commented 4 years ago

Hi -

Primary question: Do I need to re-run composer since moving files to new server?

A little over a year ago, I used your library to get my telegrambot running on IIS (Windows server). This week I moved to another server/host.

Can't seem to get it running again without errors. Here are some things I did so far:

Errors I'm getting (source code of my small PHP file are below this error section):

FROM IIS: 2020-08-11 06:36:48 GET /telegrambot/empathybot.php - 80 - 64.207.225.186 HTTP/1.1 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:79.0)+Gecko/20100101+Firefox/79.0 - empathybot.net 404 0 0 277 /telegrambot/empathybot.php - /telegrambot/empathybot.php 184.175.105.38

FROM php_errors.log: [11-Aug-2020 06:40:57 UTC] PHP Warning: include(): Failed opening '/vendor/autoload.php' for inclusion (include_path='.;C:\php\pear') in C:\home\empathybot.net\wwwroot\TelegramBot\EmpathyBot.php on line 8 [11-Aug-2020 06:40:57 UTC] PHP Fatal error: Uncaught Error: Class 'Telegram' not found in C:\home\empathybot.net\wwwroot\TelegramBot\EmpathyBot.php:18 Stack trace:

0 {main}

thrown in C:\home\empathybot.net\wwwroot\TelegramBot\EmpathyBot.php on line 18 [11-Aug-2020 06:41:47 UTC] PHP Fatal error: Uncaught TypeError: Argument 1 passed to Telegram::sendMessage() must be of the type array, string given, called in C:\home\empathybot.net\wwwroot\TelegramBot\EmpathyBot.php on line 29 and defined in C:\home\empathybot.net\wwwroot\TelegramBot\vendor\eleirbag89\telegrambotphp\Telegram.php:176 Stack trace:

0 C:\home\empathybot.net\wwwroot\TelegramBot\EmpathyBot.php(29): Telegram->sendMessage('Thinking...')

1 {main}

thrown in C:\home\empathybot.net\wwwroot\TelegramBot\vendor\eleirbag89\telegrambotphp\Telegram.php on line 176

EMPATHYBOT.PHP contents:

<?php
/**
 * Telegram Bot example.
 * @author Gabriele Grillo <gabry.grillo@alice.it>
 */
include ('/vendor/autoload.php');
// include ('./vendor/autoload.php');
// the commented out include above worked on old server. I'm testing the slight change you see here on this new server just to make sure path is working, since some of those errors above are about finding things.

// FILE GOAL
// (1) get message that was sent to @EmpathyBot in Telegram
// (2) pass message on to empathybot.net/convert... url
// (3) retrieve response from empathybot.net/convert...asp
// (4) pass this response on to Telegram chat/user

$bot_token = "[token hidden here but not on server]";
$url = "https://EmpathyBot.net/convert-empathy-go.asp?bot=yes";

$telegram = new Telegram($bot_token);

// Get message from Telegram User to Bot
$result = $telegram->getData();

// Get text and chat_id from that message array
$chat_id = $result['message'] ['chat'] ['id'];
$user_id = $result['message'] ['from'] ['id'];
$first_name = $result['message'] ['from'] ['first_name'];
$last_name = $result['message'] ['from'] ['last_name'];
$username = $result['message'] ['from'] ['username'];
$txtToSend = $result['message'] ['text'];
$txtToSend = strtolower($txtToSend);

// in strpos comparison below, it seems to be 
//   skipping first character of our submitted string
$toTest = " " . $txtToSend;
// Make sure they were addressing the bot
if (strpos($toTest, "eqbot") !=false or strpos($toTest, "empathbot") !=false or strpos($toTest, "empathybot") !=false) {
    // Send this message on to my app's $url and get response
    // $toSend = $url . "&txtMsg=" . urlencode($txtToSend);
    // $toSend = $url . "&txtMsg=" . urlencode($txtToSend) . "&id_chat=" . urlencode($chat_id) . "&id_user=" . urlencode($user_id) . "&s_name_user=" . urlencode($username) . "&s_name_first=" . urlencode($firstname) . "&s_name_last=" . urlencode($lastname) . "&mess=" . urlencode($message);
    $toSend = $url . "&txtMsg=" . urlencode($txtToSend) . "&id_chat=" . urlencode($chat_id) . "&id_user=" . urlencode($user_id) . "&s_name_first=" . urlencode($first_name) . "&s_name_last=" . urlencode($last_name) . "&s_name_user=" . urlencode($username);

    $options = array(
    'https'=>array(
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                "Cookie: foo=bar\r\n" // check function.stream-context-create on php.net
            )
        );

    $context = stream_context_create($options);
    $txtResponse = file_get_contents($toSend, false, $context);
    // $txtResponse = $txtResponse . " - debug: id_chat = " . $chat_id;
    $toTelegram = urldecode($txtResponse);

    // Pass response back to Telegram
    $content = array('chat_id' => $chat_id, 'text' => $toTelegram);
    $telegram->sendMessage($content);
}
Eleirbag89 commented 4 years ago

$telegram->sendMessage("Thinking...") The argument should be an array, I'm amazed it worked this way in IIS 😲

Look at https://github.com/Eleirbag89/TelegramBotPHP/blob/master/bot%20examples/webhook/updatecowsay.php for some examples

PS Can you format the code using the github code function in the editor ? It would be easier to read

ScotterMonk commented 4 years ago

$telegram->sendMessage("Thinking...") The argument should be an array, I'm amazed it worked this way in IIS 😲

Look at https://github.com/Eleirbag89/TelegramBotPHP/blob/master/bot%20examples/webhook/updatecowsay.php for some examples

PS Can you format the code using the github code function in the editor ? It would be easier to read THANK YOU! Apology for the formatting! I see now how bad it is (hard to read)! Edited original post. Will modify that string and see if that makes the difference.

ScotterMonk commented 4 years ago

Made no difference. I guess since you didn't mention composer, that means I don't have to rerun it on the new server for any kind of registration or other purposes? Just copying all the files from the old server should do?

Eleirbag89 commented 4 years ago

Since it's finding the files I don't think it's a problem with composer.

So you tried to use

$content` = ['chat_id' => $chat_id, 'text' => "Thinking..."];
$telegram->sendMessage($content);

$content` = ['chat_id' => $chat_id, 'text' => $txtResponse];
$telegram->sendMessage($content);

And nothing changes ? Fan you post the stacktrace after you make this changes ?

ScotterMonk commented 4 years ago

@Eleirbag89 thank you for helping so much! As I'm sure you can tell, I'm VERY inexperienced with PHP. Knowing I'm running this on a Windows server (and I have full remote access), what is the easiest method you suggest for enabling "stacktrace"? I've been searching the web and finding a HUGE number of products. Thank you for any help you can offer!

Eleirbag89 commented 4 years ago

I was thinking about some snippets of the php_errors.log file

ScotterMonk commented 4 years ago

That makes things easier! Sadly, though, the server is not writing to a log file. I suspected it might be a permissions issue but not sure. Gave modify permissions to IIS_IUSR, the web site application pool user account, and many others. Modified php.ini in both main c:\php\php7 folder, php.ini in website root, and php.ini in the folder where the PHP part of the site runs. Oh the mods:

ScotterMonk commented 4 years ago

BTW, Grimstack, I have no expectation that you me with this kind of stuff. As a fellow developer [not PHP], I totally understand how valuable your time is and not your obligation to help me with issues with my server. So yeah, you have so far gone above and beyond with how much you helped so far and I really really appreciate that!

ScotterMonk commented 4 years ago

Looking at the following code in my little PHP file that basically receives msg from telegram, sends string to my classic ASP application, receives string answer back from classic ASP, and sends it on to Telegram, I have a question:

// Send this message on to Classic ASP's $url and get response
$txtResponse = file_get_contents($url . "&txtMsg=" . urlencode($txtToSend));
// Pass response back to Telegram
$telegram->sendMessage($txtResponse);

This also seems to be an issue with using sendMessage to send non-array? Maybe it worked [totally fine] before because of older version of PHP I was using? Using 7.0 now. How can I modify that last line of code to send that one string as a PHP array?

Eleirbag89 commented 4 years ago

I'm a little bit confused. sendMessage take as input an array because the telegram API need it that way. You need to have a chat_id key the text key, like the examples I gave you early.

ScotterMonk commented 4 years ago

Sorry for confusion. I just re-checked the code and you are right. I did make that $txtToSend an array by using

$content = array('chat_id' => $chat_id, 'text' => $toTelegram);

And because the code did not change since moving servers AND since logs will not write, I think this is a server config issue; maybe permissions. I'll look again in windows logs to see if any write errors. Not sure what else to do.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.