PHP Telegram Bot:从另一个 php 文件调用函数导致 500 内部服务器错误
PHP Telegram Bot: calling functions from another php file results in 500 Internal Server Error
我正在使用 PHP 制作电报机器人。目前,我的 bot.php 文件中有很多函数,但我想将它们放在单独的文件 bot-functions.php 中。但是,当我这样做时,我的机器人开始返回 500 个内部服务器错误。
代码如下:
bot.php 具有所有功能:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
$update = json_decode(file_get_contents("php://input"), TRUE);
$filter = new Filter();
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
handleProfanity($chatId, $message, $text);
} else {
handleGoodMessage($chatId, $text, $filter);
}
}
if (array_key_exists("message", $update)) { # checks if update is a new message
# update is a new message
handleMessage($update["message"], $filter);
} elseif (array_key_exists("edited_message", $update)) { # checks if update is an edited message
# update is an edited message
handleMessage($update["edited_message"], $filter);
}
bot.php 没有函数:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
include 'bot-functions.php';
$update = json_decode(file_get_contents("php://input"), TRUE);
$filter = new Filter();
if (array_key_exists("message", $update)) { # checks if update is a new message
# update is a new message
handleMessage($update["message"], $filter);
} elseif (array_key_exists("edited_message", $update)) { # checks if update is an edited message
# update is an edited message
handleMessage($update["edited_message"], $filter);
}
bot-functions.php:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
handleProfanity($chatId, $message, $text);
} else {
handleGoodMessage($chatId, $text, $filter);
}
}
我删除了重复的包含行,它起作用了。谢谢@Unbywyd!
所以现在我的 bot-functions.php 看起来像这样:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
我正在使用 PHP 制作电报机器人。目前,我的 bot.php 文件中有很多函数,但我想将它们放在单独的文件 bot-functions.php 中。但是,当我这样做时,我的机器人开始返回 500 个内部服务器错误。
代码如下:
bot.php 具有所有功能:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
$update = json_decode(file_get_contents("php://input"), TRUE);
$filter = new Filter();
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
handleProfanity($chatId, $message, $text);
} else {
handleGoodMessage($chatId, $text, $filter);
}
}
if (array_key_exists("message", $update)) { # checks if update is a new message
# update is a new message
handleMessage($update["message"], $filter);
} elseif (array_key_exists("edited_message", $update)) { # checks if update is an edited message
# update is an edited message
handleMessage($update["edited_message"], $filter);
}
bot.php 没有函数:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
include 'bot-functions.php';
$update = json_decode(file_get_contents("php://input"), TRUE);
$filter = new Filter();
if (array_key_exists("message", $update)) { # checks if update is a new message
# update is a new message
handleMessage($update["message"], $filter);
} elseif (array_key_exists("edited_message", $update)) { # checks if update is an edited message
# update is an edited message
handleMessage($update["edited_message"], $filter);
}
bot-functions.php:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
handleProfanity($chatId, $message, $text);
} else {
handleGoodMessage($chatId, $text, $filter);
}
}
我删除了重复的包含行,它起作用了。谢谢@Unbywyd!
所以现在我的 bot-functions.php 看起来像这样:
<?php
include __DIR__ . './Filter.php';
include 'telegram-functions.php';
/**
* Sends message to user who sent the original message.
*
* @param $message
* @param $text
*/
function informUser($message, $text) {
$userId = $message['from']['id'];
sendMsg($userId, $text);
}
/**
* Sends a report to jail channel
*
* @param $message
* @param $text
* @param $reason
*/
function sendToJail($message, $text, $reason) {
$jailId = $message['chat']['id']; # edit $jailId to match jail channel id
$username = $message['from']['username'];
$report = "Message from @".$username." deleted.\nMessage: ".$text."\nReason: ".$reason;
sendMsg($jailId, $report);
}
/**
* Handles message without net id
*
* @param $chatId
* @param $message
* @param $text
*/
function handleNoId($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has no net id.");
informUser($message, "Your message has been deleted due to no net id. \nMessage sent: ".$text);
}
/**
* Handles message with profanity
*
* @param $chatId
* @param $message
* @param $text
*/
function handleProfanity($chatId, $message, $text) {
delMsg($chatId, $message['message_id']);
sendToJail($message, $text, "Message has profanity.");
informUser($message, "Your message has been deleted due to profanity(s). \nMessage sent: ".$text);
}
/**
* Handles messages that have net id and no profanities
*
* @param $chatId
* @param $text
* @param $filter
*/
function handleGoodMessage($chatId, $text, $filter) {
$id = $filter->getId($text);
$report = "Message '".$text."' passed the filters.\nID: ".$id;
sendMsg($chatId, $report);
}
/**
* Handles messages
*
* @param $message
* @param $filter
*/
function handleMessage($message, $filter) {
$text = $message["text"]; # assigns $text to message sent
$chatId= $message["chat"]["id"];
if (!($filter->hasNetId($text))) {
handleNoId($chatId, $message, $text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity