Skip to content

Commit

Permalink
#42 - CLI helper / personal assistant on command prompt
Browse files Browse the repository at this point in the history
Commit for php helper and linux/macos shell runner, outstanding is
windows batch file.

The TagUI scripts are already in natural-language-like syntax to
convert to JS code. I want to have a helper that can convert
natural-language-like instructions on the command line to call the
right scripts with right parameters.

Instead of typing tagui download_bank_report june on command prompt, I
think it is more intuitive to support typing erina download my june
bank report or something like that directly on command prompt. And let
the magic happens behind the scenes to return the results.

running this -
erina 5w1h/verb fillers options/modifiers fillers
single-or-multi-word-context

results in running -
tagui 5w1h/verb_context quiet options/modifiers

calling using chrome/firefox/headless or with chrome/firefox/headless
at the end of erina … will call tagui in visible chrome, firefox,
headless chrome respectively.

this feature allows future extension to natural language voice control.
the trigger word erina can be changed by just renaming the erina file
(macos/linux) or erina.cmd (windows) file.
  • Loading branch information
kensoh committed Aug 9, 2017
1 parent 85e77cd commit 3e3f924
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -13,6 +13,10 @@
/src/samples/8_hastebin.js
/src/samples/8_hastebin.log

# exclude generated files for cli assistant
/src/tagui_helper
/src/tagui_helper.cmd

# exclude local log files from api service
/src/tagui_service.in
/src/tagui_service.out
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "tagui",
"version": "2.2.0",
"version": "2.3.0",
"description": "General purpose tool for automating web interactions",
"keywords": [
"tagui"
Expand Down
23 changes: 23 additions & 0 deletions src/erina
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# HELPER ASSISTANT SCRIPT FOR RUNNING TAGUI FRAMEWORK ~ TEBEL.ORG #

# save location of initial directory where helper is called
initial_dir=`pwd`

# change current directory to TAGUI directory
SOURCE="${BASH_SOURCE[0]}" # slimerjs's implementation instead of cd "`dirname "$0"`"
while [ -h "$SOURCE" ]; do TAGUI_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"; [[ $SOURCE != /* ]] && SOURCE="$TAGUI_DIR/$SOURCE"; done
TAGUI_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"; cd "$TAGUI_DIR"

# delete helper file if it exists to prevent running an old call
if [ -f "tagui_helper" ]; then rm tagui_helper; fi

# call php helper to interpret parameters passed in to cli helper
php -q tagui_helper.php $1 $2 $3 $4 $5 $6 $7 $8 $9

# run generated output from php helper to call automation flow
if [ -f "tagui_helper" ]; then ./tagui_helper; fi

# change back to initial directory where tagui is called
cd "$initial_dir"
4 changes: 2 additions & 2 deletions src/tagui
Expand Up @@ -6,7 +6,7 @@ chrome_command="google-chrome"

if [ -z "$1" ]
then
echo "tagui v2.2: use following syntax and below options to run ./tagui flow_filename option(s)"
echo "tagui v2.3: use following syntax and below options to run ./tagui flow_filename option(s)"
echo
echo "IMPORTANT: SAVE YOUR WORK BEFORE USING CHROME OR HEADLESS, TAGUI WILL RESTART CHROME"
echo "headless - run on invisible Chrome web browser instead of default PhantomJS (first install Chrome)"
Expand Down Expand Up @@ -185,7 +185,7 @@ if [ -f "tagui.sikuli/tagui_sikuli.in" ]; then rm tagui.sikuli/tagui_sikuli.in;
if [ -f "tagui.sikuli/tagui_sikuli.out" ]; then rm tagui.sikuli/tagui_sikuli.out; fi

# delete chrome / headless chrome integration files if they exist
if [ -f "tagui_chrome.in" ]; then rm tagui_chrome.in; fi;
if [ -f "tagui_chrome.in" ]; then rm tagui_chrome.in; fi
if [ -f "tagui_chrome.out" ]; then rm tagui_chrome.out; fi

# default exit code 0 to mean no error parsing automation flow file
Expand Down
2 changes: 1 addition & 1 deletion src/tagui.cmd
Expand Up @@ -8,7 +8,7 @@ rem enable windows for loop advanced flow control
setlocal enableextensions enabledelayedexpansion

if "%~1"=="" (
echo tagui v2.2: use following syntax and below options to run - tagui flow_filename option^(s^)
echo tagui v2.3: use following syntax and below options to run - tagui flow_filename option^(s^)
echo.
echo IMPORTANT: SAVE YOUR WORK BEFORE USING CHROME OR HEADLESS, TAGUI WILL RESTART CHROME
echo headless - run on invisible Chrome web browser instead of default PhantomJS ^(first install Chrome^)
Expand Down
75 changes: 75 additions & 0 deletions src/tagui_helper.php
@@ -0,0 +1,75 @@
<?php

/* HELPER PARSER SCRIPT FOR TAGUI FRAMEWORK ~ TEBEL.ORG */

// configure directory to look for automation flow files - default location is set to tagui/flows
// avoid spaces in folder and filename, issue with CasperJS and TagUI can't tell where options start
$helper_flow_directory = "../flows";

// concatenate all parameters into one string for processing
$raw_intent = $argv[1] . " " . $argv[2] . " " . $argv[3] . " " . $argv[4] . " " .
$argv[5] . " " . $argv[6] . " " . $argv[7] . " " . $argv[8] . " " . $argv[9];
$raw_intent = trim($raw_intent);

// initialise components required for forming the final output command
$helper_flow_context = ""; $helper_flow_action = ""; $helper_flow_filename = "";
$helper_flow_options = ""; $helper_flow_browser = ""; $helper_flow_found = false;

// remove filler words that have limited impact on meaning of intention
$raw_intent = str_replace(" is "," ",$raw_intent); $raw_intent = str_replace(" are "," ",$raw_intent);
$raw_intent = str_replace(" was "," ",$raw_intent); $raw_intent = str_replace(" were "," ",$raw_intent);
$raw_intent = str_replace(" my "," ",$raw_intent); $raw_intent = str_replace(" me "," ",$raw_intent);
$raw_intent = trim($raw_intent);

// check for using browser string at end of intention to pass browser type option
// important to concatenate at end of options in order to not disrupt p1-p9 parameters
if ((strpos($raw_intent,"using chrome")!==false) and
(strrpos($raw_intent,"using chrome") == (strlen($raw_intent) - strlen("using chrome"))))
{$helper_flow_browser = " chrome"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"using chrome"));}
else if ((strpos($raw_intent,"using headless")!==false) and
(strrpos($raw_intent,"using headless") == (strlen($raw_intent) - strlen("using headless"))))
{$helper_flow_browser = " headless"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"using headless"));}
else if ((strpos($raw_intent,"using firefox")!==false) and
(strrpos($raw_intent,"using firefox") == (strlen($raw_intent) - strlen("using firefox"))))
{$helper_flow_browser = " firefox"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"using firefox"));}
else if ((strpos($raw_intent,"with chrome")!==false) and
(strrpos($raw_intent,"with chrome") == (strlen($raw_intent) - strlen("with chrome"))))
{$helper_flow_browser = " chrome"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"with chrome"));}
else if ((strpos($raw_intent,"with headless")!==false) and
(strrpos($raw_intent,"with headless") == (strlen($raw_intent) - strlen("with headless"))))
{$helper_flow_browser = " headless"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"with headless"));}
else if ((strpos($raw_intent,"with firefox")!==false) and
(strrpos($raw_intent,"with firefox") == (strlen($raw_intent) - strlen("with firefox"))))
{$helper_flow_browser = " firefox"; $raw_intent = substr($raw_intent,0,strrpos($raw_intent,"with firefox"));}
$raw_intent = trim($raw_intent);

// throw error message if there are no minimum of an action + 1-word context
if (strpos($raw_intent," ")==false) die("cannot find action and context in your instruction" . "\n");

// loop through intention after filtering away fillers and browser option
$token = explode(" ",$raw_intent); $helper_flow_action = $token[0]; // split into tokens separated by space
for ($count = 1; $count < sizeof($token); $count++) {$helper_flow_context = ""; $helper_flow_options = "";
for ($context_count = 1 + $count; $context_count <= sizeof($token); $context_count++) {
$helper_flow_context .= $token[$context_count - 1] . " ";}
$helper_flow_context = str_replace(" ","_",trim($helper_flow_context));
for ($options_count = 1 ; $options_count < $count; $options_count++) {
$helper_flow_options .= $token[$options_count] . " ";}
$helper_flow_options = trim($helper_flow_options);
$helper_flow_filename = $helper_flow_directory . "/" . trim($helper_flow_action . "_" . $helper_flow_context);
// echo "./tagui " . $helper_flow_filename . " quiet " . trim($helper_flow_options . $helper_flow_browser) . "\n";
if (file_exists($helper_flow_filename)) {$helper_flow_found = true; break;}}

// generate flow options taking into account browser to be used
$helper_flow_options = trim($helper_flow_options . $helper_flow_browser);

// throw error message if a match for flow filename is not found
if (!$helper_flow_found) die("cannot find automation to run for your instruction" . "\n");

// write interpreted command and flow to run to output runner
$output_file = fopen('tagui_helper','w') or die("cannot open tagui_helper, raise an issue on TagUI GitHub page" . "\n");
fwrite($output_file,"#!/usr/bin/env bash" . "\n");
fwrite($output_file,"# GENERATED HELPER ASSISTANT SCRIPT FOR RUNNING TAGUI FRAMEWORK ~ TEBEL.ORG #" . "\n");
fwrite($output_file,"./tagui " . $helper_flow_filename . " quiet " . $helper_flow_options . "\n");
fclose($output_file); chmod ('tagui_helper',0700);

?>

1 comment on commit 3e3f924

@kensoh
Copy link
Member Author

@kensoh kensoh commented on 3e3f924 Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tagged wrong issue, should be #44

Please sign in to comment.