<commit_before><?php

namespace Telegram\Bot\Response;

use DB;
use Telegram as B;
use Telegram\Bot\Bot;

class Notification
{
	private $ndata = [];

	public function __construct(Bot $b)
	{
		$this->b = $b;
		$this->buildContext();
	}

	public function run()
	{
		if ($this->shouldReport()) {
			$this->notify();
		}
	}

	private function isKnownUser($v, $type = "username")
	{
		$st = DB::prepare("SELECT `user_id` FROM `a_users` WHERE `{$type}` LIKE :bind LIMIT 1;");
		$st->execute([":bind" => $v]);
		$st = $st->fetch(PDO::FETCH_NUM);
		if ($st) {
			return $st[0];
		} else {
			return false;
		}
	}

	private function shouldReport()
	{
		var_dump($this->mention);die;
		foreach ($this->mention as $val) {
			foreach ($val['username'] as $v) {
				$v =  $this->isKnownUser($v);
				$v and $this->buildReply("mention", $v);
			}
			foreach ($val['user_id'] as $v) {
				$v =  $this->isKnownUser($v, "user_id");
				$v and $this->buildReply("mention", $v);
			}
		}
		return sizeof($this->ndata);
	}

	private function buildReply($type, $userid)
	{
		$msg = $this->buildMessage($type);
		$this->ndata[] = [
			"chat_id" => $userid,
			"text"  => $msg,
			"parse_mode" => "HTML"
		];
	}

	private function buildMessage($type)
	{
		$name = "<a href=\"tg://user?id={$this->b->user_id}\">".htmlspecialchars($this->b->name, ENT_QUOTES, 'UTF-8')."</a>";
		if ($this->b->chatuname) {
			$chatroom = "<a href=\"https://t.me/{$this->b->chatuname}/{$this->b->msgid}\">".htmlspecialchars($this->b->chattitle)."</a>";
		} else {
			$chatroom = "<b>".$this->b->chattitle."</b>";
		}
		switch ($type) {
			case 'mention':
				return "{$name} tagged you in {$chatroom}\n\n<pre>".htmlspecialchars($this->b->text)."</pre>";
				break;
			
			default:
				break;
		}
	}

	private function buildContext()
	{
		$getUsername = []; $getId = [];
		foreach ($this->b->entities as $val) {
			if ($val['type'] === "mention") {
				$getUsername[] = substr($this->lowertext, $val['offset'], $val['length']);
			} elseif ($val['type'] === "text_mention" && $val['user']['is_bot'] === false) {
				$getID[] = $val['user']['id'];
			}
		}
		$this->mention = [
			"username" => $getUsername,
			"user_id" => $getId
		];
		$this->replyto = [

		];
	}

	private function notify()
	{
		foreach ($this->ndata as $val) {
			B::sendMessage($val);
		}
	}
}<commit_msg>Fix undefined property<commit_after><?php

namespace Telegram\Bot\Response;

use DB;
use Telegram as B;
use Telegram\Bot\Bot;

class Notification
{
	private $ndata = [];

	public function __construct(Bot $b)
	{
		$this->b = $b;
		$this->buildContext();
	}

	public function run()
	{
		if ($this->shouldReport()) {
			$this->notify();
		}
	}

	private function isKnownUser($v, $type = "username")
	{
		$st = DB::prepare("SELECT `user_id` FROM `a_users` WHERE `{$type}` LIKE :bind LIMIT 1;");
		$st->execute([":bind" => $v]);
		$st = $st->fetch(PDO::FETCH_NUM);
		if ($st) {
			return $st[0];
		} else {
			return false;
		}
	}

	private function shouldReport()
	{
		var_dump($this->mention);die;
		foreach ($this->mention as $val) {
			foreach ($val['username'] as $v) {
				$v =  $this->isKnownUser($v);
				$v and $this->buildReply("mention", $v);
			}
			foreach ($val['user_id'] as $v) {
				$v =  $this->isKnownUser($v, "user_id");
				$v and $this->buildReply("mention", $v);
			}
		}
		return sizeof($this->ndata);
	}

	private function buildReply($type, $userid)
	{
		$msg = $this->buildMessage($type);
		$this->ndata[] = [
			"chat_id" => $userid,
			"text"  => $msg,
			"parse_mode" => "HTML"
		];
	}

	private function buildMessage($type)
	{
		$name = "<a href=\"tg://user?id={$this->b->user_id}\">".htmlspecialchars($this->b->name, ENT_QUOTES, 'UTF-8')."</a>";
		if ($this->b->chatuname) {
			$chatroom = "<a href=\"https://t.me/{$this->b->chatuname}/{$this->b->msgid}\">".htmlspecialchars($this->b->chattitle)."</a>";
		} else {
			$chatroom = "<b>".$this->b->chattitle."</b>";
		}
		switch ($type) {
			case 'mention':
				return "{$name} tagged you in {$chatroom}\n\n<pre>".htmlspecialchars($this->b->text)."</pre>";
				break;
			
			default:
				break;
		}
	}

	private function buildContext()
	{
		$getUsername = []; $getId = [];
		foreach ($this->b->entities as $val) {
			if ($val['type'] === "mention") {
				$getUsername[] = substr($this->b->lowertext, $val['offset'], $val['length']);
			} elseif ($val['type'] === "text_mention" && $val['user']['is_bot'] === false) {
				$getID[] = $val['user']['id'];
			}
		}
		$this->mention = [
			"username" => $getUsername,
			"user_id" => $getId
		];
		$this->replyto = [

		];
	}

	private function notify()
	{
		foreach ($this->ndata as $val) {
			B::sendMessage($val);
		}
	}
}
{'fraction_non_alphanumeric': 0.2399486740804106, 'fraction_numerical': 0.0012831479897348161, 'mean_word_length': 3.5144787644787643, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 106, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4812416', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1819, 'n_words': 332}