(GUIDE) How to: Add custom command to Close/Open Chat

136 views
Skip to first unread message

FireWire

unread,
Feb 24, 2014, 12:48:51 AM2/24/14
to
Hi.

I received a request to share my solution for a custom command to close/open the chat. So I will post this so everyone can use my modification.

If you don't have added any custom commands, follow Guide no:1.
If you already have one or more custom commands, follow Guide no:2.

Guide no:1(no previously added custom commands)

1. Open 'chat/lib/class/CustomAJAXChat.php'.

2. At the bottom of the opened file(before the last '}' and '?>'(if present), add this code:
function parseCustomCommands($text, $textParts) {
        if($this->getUserRole() == AJAX_CHAT_ADMIN) {
               
switch($textParts[0]) {
               case '/toggleChat':
                     $config_file = AJAX_CHAT_PATH.'lib/config.php';
                $file_contents = file_get_contents($config_file);
                      $open = "\$config['chatClosed'] = false;";
                     $closed = "\$config['chatClosed'] = true;";
                    if(strpos($file_contents, $open)) {
                            $file_contents = str_replace($open, $closed, $file_contents);
                          file_put_contents($config_file, $file_contents);
                               $this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
                   } else {
                               $file_contents = str_replace($closed, $open, $file_contents);
                          file_put_contents($config_file, $file_contents);
                               $this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
                    }
             return true;
              default: return false;
              }
       
}
}


3. Open 'chat/js/custom.js' and add this code:

ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
        case '/open': // Chat open confirmation message
     text = text.replace('/open', ' ');
     return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
     break;
case '/close': // Chat close confirmation message
     text = text.replace('/close', ' ');
     return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
     break;
        }
}

4. Open 'chat/js/language/*your preferred language files*.js' and find the first code in the file:
var ajaxChatLang = {

Add this code directly after this, before the first translated word:
chatOpened: 'Chat is opened!',
chatClosed: 'Chat is closed!',
(Make sure to open all language files you are using and add the code above.)

5. Go to "CONCLUSION" below the next guide!


Guide no:2(one or more previously added custom commands)

1. Open 'chat/lib/class/CustomAJAXChat.php'.

2. Find:
function parseCustomCommands($text, $textParts) {

In that code search for:
default: 
return false;
}
And remove
default:
return false;
but not the "}"!
After the closing you should add your code(3)!

Example on were to put the code:
function parseCustomCommands($text, $textParts) {
switch($textParts[0]) {
 *A bunch of code here*
 *Look for the last closing "}", add the new code after the closing.*
} <--- * The last closing for "switch" *
<--- *Here is were you will add your new code!*
}


3. Now add this code at the position mentioned above:
        if($this->getUserRole() == AJAX_CHAT_ADMIN) {
               
switch($textParts[0]) {
               case '/toggleChat':
$config_file = AJAX_CHAT_PATH.'lib/config.php';
$file_contents = file_get_contents($config_file);
$open = "\$config['chatClosed'] = false;";
$closed = "\$config['chatClosed'] = true;";
if(strpos($file_contents, $open)) {
$file_contents = str_replace($open, $closed, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
} else {
$file_contents = str_replace($closed, $open, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
}
       return true;
               default: return false;
               }
        
}

4. Open 'chat/js/custom.js'. Check if you have the function:
ajaxChat.replaceCustomCommands = function(text, textParts) {

If NOT, add this code:
ajaxChat.replaceCustomCommands = function(text, textParts) {
switch(textParts[0]) {
        case '/open': // Chat open confirmation message
     text = text.replace('/open', ' ');
     return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
     break;
case '/close': // Chat close confirmation message
     text = text.replace('/close', ' ');
     return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
     break;
        }
}

If PRESENT, then find this:
default:
return text;

Before that code, add this:
case '/open': // Chat open confirmation message
     text = text.replace('/open', ' ');
     return '<span class="chatBotMessage">' + this.lang['chatOpened'] + '</span>';
     break;
case '/close': // Chat close confirmation message
     text = text.replace('/close', ' ');
     return '<span class="chatBotErrorMessage">' + this.lang['chatClosed'] + '</span>';
     break;


5. Open 'chat/js/language/*your preferred language files*.js' and find the first code in the file:
var ajaxChatLang = {

Add this code directly after this, before the first translated word:
chatOpened: 'Chat is opened!',
chatClosed: 'Chat is closed!',
(Make sure to open all language files you are using and add the code above and change the text inside the ' ' to the correct translation for the language file.)


CONCLUSION:
The code checks if the chat is opened or closed and triggers the opposite eg. if chat is opened, it closes. If chat is closed, it opens.
Now everything is done and you can use the command '/toggleChat' to toggle between opened or closed chat. Only Admins can use this command. When the command is issued, the chat automatically log out all users including Mods. Only exception is Admins. I don't provide code for changing this, because this is built in behavior in the chat and requires changing code inside 'AjaxChat.php', which this guide don't cover. But if you like to change this, you can do so by searching for the function 'isChatOpen()'. This function controls this behavior.

Hope I didn't make to much typos or errors. I have the code embedded in a lot of other custom changes, so was a bit messy to pick out the parts for this command only. Hopefully I haven't missed anything.
Any questions, please throw them at me, will try to help as far as I can.


Best regards,
FireWire

Frug

unread,
Feb 13, 2014, 5:40:33 PM2/13/14
to
There's no parseCustomCommands override by default in CustomAJAXChat.php. You must be referring to one of the wiki tutorials or looking at some code you've gotten from somewhere else.

Anyway, chat won't explode if you don't return false by default, but it's there for a reason. Check inside AJAXChat.php for how parseCustomCommands is actually used:

if(!$this->parseCustomCommands($text, $textParts)) {
    $this
->insertChatBotMessage(
        $this
->getPrivateMessageID(),
       
'/error UnknownCommand '.$textParts[0]
   
);
}



Chat expects parseCustomCommands to return false if the command entered by the user is not recognized. You could return true by default, and thus allow users to type /whatever without any error message that the command is not recognized.

I would leave it in because it's bad form not to have it there. First because a switch statement should probably always have a default case ( if it's none of the options, then what? ) and second because returning null only works because php is treating it as a "falsey" value, but it's not technically the same as returning false. As a dev I don't think you should get used to php's implicit type conversion.

If some day i go in and refactor it from
if(!$this->parseCustomCommands($text, $textParts))
to
if($this->parseCustomCommands($text, $textParts) === false)

your custom file would stop giving errors for junk commands.
default:
return false;
And remove that code!

Now find the last closing( } ) in that function, example below:
function parseCustomCommands($text, $textParts) {

 *A bunch of code here*
 *Look for the last closing "}" in this function, the new code should be added before that.*


<--- *Here is were you will add your new code!*
}
3. Now add this code at the position mentioned above:
        if($this->getUserRole() == AJAX_CHAT_ADMIN) {
               
switch($textParts[0]) {
               case '/toggleChat':
$config_file = AJAX_CHAT_PATH.'lib/config.php';
$file_contents = file_get_contents($config_file);
$open = "\$config['chatClosed'] = false;";
$closed = "\$config['chatClosed'] = true;";
if(strpos($file_contents, $open)) {
$file_contents = str_replace($open, $closed, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
} else {
$file_contents = str_replace($closed, $open, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
}
       return true;
       
        }
        
}

TO FRUG IF YOU READ THIS: I have a question about the 'default:return false;' in 'CustomAjaxChat.php'. Is it really needed? I have removed this, because everything seems to work anyway. It makes adding custom commands a bit harder, because it needs to be moved to the last added custom command to work as intended. Failure to do so, kills all other custom commands added after that code.
My conclusion is that when the chat checks the "parseCustomCommands" and don't find a matching command and/or the issuer don't have permission to issue the command, it seems that the "parseCustomCommands" returns to the "insertParsedMessage" function in AjaxChat.php anyway and issues a "/error UnknownCommand". So is there something that can go wrong by removing that part? Please notify me if that's the case.

Best regards,
FireWire

Frug

unread,
Feb 13, 2014, 5:51:10 PM2/13/14
to ajax...@googlegroups.com
One way I guess I could make custom commands easier for people on the php side would be to store them as an array() and then, instead of using a switch statement, check to see if x is in the array.

Since we're lucky enough to be using php 5.3 it's now possible to do something I used to only be able to do in javascript:

$customCommands = array(
    'toggleChat' => function() {

        $config_file = AJAX_CHAT_PATH.'lib/config.php';
        $file_contents = file_get_contents($config_file);
        $open = "\$config['chatClosed'] = false;";
        $closed = "\$config['chatClosed'] = true;";
        ...
    }
);

I'm pretty sure this would work well. I'd have to modify the core to search through this array for matching commands, but that shouldn't be hard.

Frug

unread,
Feb 13, 2014, 5:57:17 PM2/13/14
to ajax...@googlegroups.com

MoXAri Apph

unread,
Feb 23, 2014, 10:25:17 PM2/23/14
to ajax...@googlegroups.com
I don't quite know what I'm doing wrong, but I followed guide 2, and I don't think there should be anything wrong, but when I type /toggleChat, it says it's unknown command

For CustomAjaxChat.php I have

   function parseCustomCommands($text, $textParts) {
       switch($textParts[0]) {

           // Display userIP:
            case '/myip':
                $this->insertChatBotMessage(
                    $this->getPrivateMessageID(),
                    '/myip '.$this->getSessionIP()
                );
                return true;

        if($this->getUserRole() == AJAX_CHAT_ADMIN) {
               switch($textParts[0]) {
               case '/toggleChat':
$config_file = AJAX_CHAT_PATH.'lib/config.php';
$file_contents = file_get_contents($config_file);
$open = "\$config['chatClosed'] = false;";
$closed = "\$config['chatClosed'] = true;";
if(strpos($file_contents, $open)) {
$file_contents = str_replace($open, $closed, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/close');
} else {
$file_contents = str_replace($closed, $open, $file_contents);
file_put_contents($config_file, $file_contents);
$this->insertChatBotMessage($this->getPrivateMessageID(), '/open');
}
      return true;
               default: return false;
               }

        }
        }
           
    }

For the custom.js I have
/*
// Example - Overriding the replaceCustomCommands method:
ajaxChat.replaceCustomCommands = function(text, textParts) {
return text;
}
 */
              default: return false;
              }
       
}
}

               default: return false;
               }
        
}
Best regards,
FireWire

FireWire

unread,
Feb 24, 2014, 12:57:17 AM2/24/14
to
Hi.

The error you made is that you didn't read the guide thoroughly enough. In your customAJAXChat.php you have a custom command already in place.
switch($textParts[0]) {

                    // Display userIP:
            case '/myip':
               $this->insertChatBotMessage(
                   $this->getPrivateMessageID(),
                   '/myip '.$this->getSessionIP()
               );
               return true;

But you have put my custom command before the closing tag "}" of the first command. That closing tag is now below both the "/myip" command and my custom command.
So you must move the next to last "}" and put after the:
switch($textParts[0]) {

                    // Display userIP:
           case '/myip':
              $this->insertChatBotMessage(
                  $this->getPrivateMessageID(),
                  '/myip '.$this->getSessionIP()
              );
              return true;
} <-------- THIS IS MISSING, SO MOVE THE NEXT TO LAST "}" IN THE CODE AND PLACE IT HERE.
<--------------------------------- HERE YOU PLACE MY CODE



 I tried to make the guide more easy to understand, but it's not easy to be more clear on how to do this. The user needs to have at least a little bit experience of coding in php. So I don't think I can make it any simpler than this.
Hope you understand how to fix this now.

//FireWire

MoXAri Apph

unread,
Feb 24, 2014, 3:49:40 PM2/24/14
to ajax...@googlegroups.com
Alright thanks a ton, made the fix and it works now, I actually have very little experience with php, so, there's that, glad I didn't mess up too badly xD


On Sunday, February 23, 2014 11:39:51 PM UTC-6, FireWire wrote:
Hi.

The error you made is that you didn't read the guide thoroughly enough. In your customAJAXChat.php you have a custom command already in place.
switch($textParts[0]) {

                    // Display userIP:
            case '/myip':
               $this->insertChatBotMessage(
                   $this->getPrivateMessageID(),
                   '/myip '.$this->getSessionIP()
               );
               return true;

But you have put my custom command before the closing tag "}" of the first command. That closing tag is now below both the "/myip" command and my custom command.
So you must move the next to last "}" and put after the:
switch($textParts[0]) {

                    // Display userIP:
           case '/myip':
              $this->insertChatBotMessage(
                  $this->getPrivateMessageID(),
                  '/myip '.$this->getSessionIP()
              );
              return true;

} <-------- THIS IS MISSING, SO MOVE THE NEXT TO LAST "}" IN THE CODE AND PLACE IT HERE.
<--------------------------------- HERE YOU PLACE MY CODE



 I tried to make the guide more easy to understand, but it's not easy to be more clear on how to do this. The user needs to have at least a little bit experience of coding in php. So I don't think I can make it any simpler than this.
Hope you understand how to fix this now.

//FireWire



On Monday, February 24, 2014 4:25:17 AM UTC+1, MoXAri Apph wrote:

FireWire

unread,
Feb 25, 2014, 7:16:47 AM2/25/14
to
No problem. Glad I could help you to get this working.

//FireWire
Reply all
Reply to author
Forward
0 new messages