<?php
class IntentService extends BaseSerevice {
public $miniCurl;
public $availableIntents;
public $availableSystemIntents;
public $availableSingleWordIntents;
public $inlineIntents;
public $models = [];
function __construct($tg){
parent::__construct($tg);
// $this->miniCurl = new MiniCurl("http://bot.bayadic.com:5005/model/parse");
$this->init();
}
public function init(){
$this->availableIntents= [];
$this->availableSingleWordIntents[]= new DateConverter($this);
$this->availableIntents[]= new DateConverter($this);
$this->availableIntents[]= new EuroToman($this);
$this->availableIntents[]= new TomanEuro($this);
$this->availableIntents[]= new DigitToText($this);
$this->availableIntents[]= new DeFaText($this);
$this->availableIntents[]= new FaDeText($this);
$this->availableIntents[]= new DeDeText($this);
$this->availableIntents[]= new StartPatreonCode($this);
$this->availableSystemIntents[]= new Start($this);
$this->inlineIntents[]= new InviteInline($this);
$this->inlineIntents[]= new AskJoinInline($this);
$this->inlineIntents[]= new EuroToman($this);
$this->inlineIntents[]= new TomanEuro($this);
$this->inlineIntents[]= new DigitToText($this);
}
/**
* @return Intent[]
*/
public function findIntents($text) {
$intents[]= new Intent($this);
if($this->getTG()->chat_id == "5896316179" || $text == "hah"){
$this->getTG()->sendMessageLog($text);
}
$this->askSystem($text);
if(
!$this->getTG()->isSingleWord($text)
|| is_numeric($text)
|| preg_match("/(^[۰-۹]*$)/i",$text)
) {
$intents=array_merge($intents, $this->extractIntentFrom($this->availableIntents, $text));
}
else {
$intents=array_merge($intents, $this->extractIntentFrom($this->availableSingleWordIntents, $text));
}
$this->checkAdminCommands();
return $intents;
}
/**
* @return Intent[]
*/
public function extractIntentFrom($intentsArray, $text) {
$foundIntents=[];
foreach($intentsArray as $intent){
foreach($intent->patterns as $pattern){
foreach($pattern->orRules as $orRule){
$pregResult = preg_match($orRule, $text, $match);
if($pregResult){
if(isset($match[0])){
if(isset($match[1])){
$intent->data["entities"][0]["value"] = $match[1];
}
$intent->usedPattern = $pattern;
if(!$this->isAddedToList($foundIntents, $intent)){
$foundIntents[] = $intent;
}
echo "found in or rule ".$orRule. " for intent ".$intent->name;
}
}
break;
}
$andRulesGenerally = false;
foreach($pattern->andRules as $andRule){
$andRulesGenerally = true;
$pregResult = preg_match($andRule, $text, $match);
echo "checking and rule".$andRule." ".$pregResult.PHP_EOL;
if(!$pregResult){
$andRulesGenerally = false;
break;
}
}
if($andRulesGenerally && isset($match[0])){
if(isset($match[1])){
$intent->data["entities"][0]["value"] = $match[1];
}
$intent->usedPattern = $pattern;
if(!$this->isAddedToList($foundIntents, $intent)){
$foundIntents[] = $intent;
}
echo "intent found for ".$pattern->name;
}
}
}
return $foundIntents;
}
private function isAddedToList($intentsStack, $intent){
foreach($intentsStack as $addedIntent){
if($addedIntent->name === $intent->name){
return true;
}
}
return false;
}
public function findInlineIntent($update){
// $this->askInlineSystem($text);
$userID = $update['inline_query']['from']['id'];
$text = $update['inline_query']['query'];
if (!$this->getTG()->getAccessManager()->isMemberOfChannel($userID)) {
$text= "askjoininline";
}
if(!$this->getTG()->isSingleWord($text)
|| is_numeric($text)
|| preg_match("/(^[۰-۹]*$)/i",$text)) {
$intents = $this->extractIntentFrom($this->inlineIntents, $text);
if(sizeof($intents) > 0){
foreach($intents as $intent){
$intent->actInline();
}
return true;
}
}
foreach($this->inlineIntents as $inlineIntent){
if($text == $inlineIntent->getName()){
$foundIntent = $inlineIntent->setData($update);
$foundIntent->act();
return true;
}
}
// if(!isset($foundIntent) && !isset($intent)){
if(!isset($foundIntent) && !isset($intent)){
$intent = new TranslateInline($this);
$intent->setData($update)->act();
}
}
public function askSystem($text){
foreach($this->availableSystemIntents as $intent){
// if($text==$intent->getName()){
if(strpos($text, $intent->getName()) !== false){
$foundIntent = $intent->setData($text);
$foundIntent->act();
return true;
}
}
}
public function askNLU($text){
$data["text"]=$text;
$result = $this->miniCurl->postData(json_encode($data));
$intentData = json_decode($result["body"], true);
$foundIntent = null;
foreach($this->availableIntents as $intent){
if($intentData["intent"]["name"]==$intent->getName()){
$foundIntent = $intent->setData($intentData);
$foundIntent->act();
}
}
if(!isset($foundIntent)){
if($this->getTG()->isAdmin()){
$this->getTG()->sendMessageLog(var_export($result["body"],true));
}
}
}
public function checkAdminCommands(){
if($this->getTG()->isAdmin() || ($this->getTG()->chat_id == 611059896)) {
if(strcmp($this->getTG()->message['text'], "clean") == 0){
$result = $this->db->copyAndTruncate();
}
if(strcmp($this->getTG()->message['text'], "count") == 0) {
$result = $this->db->getRowNumber("user_activities");
}
if(strcmp($this->getTG()->message['text'], "advertise") == 0) {
$numberOfPeople = 300;
$this->getTG()->sendMessage("Going to advertise",$this->getTG()->sobhanId);
$this->getTG()->sendMessage("Going to advertise",$this->getTG()->adminId);
$this->getTG()->getAdsManager()->groupAdvertise($numberOfPeople);
$this->getTG()->sendMessage("advertise finished",$this->getTG()->sobhanId);
$this->getTG()->sendMessage("advertise finished",$this->getTG()->adminId);
}
}
}
}