问题描述
我正在使用PHP制作电报机器人。目前,我的bot.PHP文件中有很多功能,但我想将它们放在单独的文件bot-functions.PHP中。但是,这样做时,我的机器人程序开始返回500个内部服务器错误。
这是代码:
<?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,"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 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,$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,$text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity
handleProfanity($chatId,$text);
} else {
handleGoodMessage($chatId,$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);
}
<?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",$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,$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,"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 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,$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,$text);
} elseif ($filter->hasProfanity($text)) { # checks if message has $profanity
# message contains profanity