<?php
class TgNotify extends Simpla
{
private function exec_curl_request($handle) {
$response = curl_exec($handle);
if ($response === false) {
$errno = curl_errno($handle);
$error = curl_error($handle);
error_log("Curl returned error $errno: $error\n");
curl_close($handle);
return false;
}
$http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
if ($http_code >= 500) {
// do not wat to DDOS server if something goes wrong
sleep(10);
return false;
} else if ($http_code != 200) {
$response = json_decode($response, true);
error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
if ($http_code == 401) {
throw new Exception('Invalid access token provided');
}
return false;
} else {
$response = json_decode($response, true);
if (isset($response['description'])) {
error_log("Request was successfull: {$response['description']}\n");
}
$response = $response['result'];
}
return $response;
}
public function apiRequest($method, $parameters) {
if (!is_string($method)) {
error_log("Method name must be a string\n");
return false;
}
if (!$parameters) {
$parameters = array();
} else if (!is_array($parameters)) {
error_log("Parameters must be an array\n");
return false;
}
foreach ($parameters as $key => &$val) {
// encoding to JSON array parameters, for example reply_markup
if (!is_numeric($val) && !is_string($val)) {
$val = json_encode($val);
}
}
$url = $this->settings->tg_apiurl.$this->settings->tg_token.'/'.$method.'?'.http_build_query($parameters);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_TIMEOUT, 60);
return $this->exec_curl_request($handle);
}
public function message($order_id){
if(!($order = $this->orders->get_order(intval($order_id))))
return false;
$purchases = $this->orders->get_purchases(array('order_id'=>$order->id));
$products_ids = array();
$variants_ids = array();
foreach($purchases as $purchase)
{
$products_ids[] = $purchase->product_id;
$variants_ids[] = $purchase->variant_id;
}
$products = array();
foreach($this->products->get_products(array('id'=>$products_ids)) as $p)
$products[$p->id] = $p;
$variants = array();
foreach($this->variants->get_variants(array('id'=>$variants_ids)) as $v)
{
$variants[$v->id] = $v;
$products[$v->product_id]->variants[] = $v;
}
foreach($purchases as &$purchase)
{
if(!empty($products[$purchase->product_id]))
$purchase->product = $products[$purchase->product_id];
if(!empty($variants[$purchase->variant_id]))
$purchase->variant = $variants[$purchase->variant_id];
}
$currency=$this->money->get_currency('RUR');
$total=$this->money->convert($order->total_price).' '.$currency->sign;
if ($order->paid == 1){$text_string='Заказ №'.$order->id.' оплачен'.PHP_EOL; }
else {$text_string='У вас новый заказ №'.$order->id.' на сумму '.$total.PHP_EOL;}
$order_labels = array();
if(isset($order->id))
foreach($this->orders->get_order_labels($order->id) as $ol)
$order_labels[] = $ol->id;
if (in_array(13, $order_labels)) { //ID метки позвонить
$text_string.=' <b>позвонить по заказу!</b> '.PHP_EOL;
}
if ($order->status == 0)
$status='ждет обработки';
elseif ($order->status == 1)
$status='в обработке';
elseif ($order->status == 4)
$status='подтвержден';
elseif ($order->status == 2)
$status='выполнен';
$text_string.='<b>Статус заказа:</b> '.$status.PHP_EOL;
if ($order->paid == 1)
$paid='оплачен';
else {$paid='не оплачен';}
$text_string.='<b>Статус оплаты:</b> '.$paid.PHP_EOL;
if($order->name)
$text_string.='<b>Клиент:</b> '.$order->name.PHP_EOL;
if($order->phone)
$text_string.='<b>Телефон:</b> '.$order->phone.PHP_EOL;
if($order->email)
$text_string.='<b>E-mail:</b> '.$order->email.PHP_EOL;
if($order->address)
$text_string.='<b>Адрес:</b> '.$order->address.PHP_EOL;
if($order->comment)
$text_string.='<b>Комментарий:</b> '.$order->comment.PHP_EOL;
$text_string.='<b>Клиент заказал:</b> '.PHP_EOL;
foreach ($purchases as &$purchase){
$text_string.=$purchase->product->brand.' '.$purchase->product_name.', '.$purchase->variant_name.' - ';
//$text_string.=':'.$purchase->grinding.PHP_EOL;
$item_price=$this->money->convert($purchase->price).' '.$currency->sign;
$text_string.=$purchase->amount.' x '.$item_price.PHP_EOL;
}
$delivery = $this->delivery->get_delivery($order->delivery_id);
$text_string.='<b>Доставка:</b> '.$delivery->name.PHP_EOL;
$delivery_price=$this->money->convert($order->delivery_price).' '.$currency->sign;
$text_string.='<b>Стоимость:</b> '.$delivery_price;
$this->apiRequest("sendMessage", array('chat_id' => $this->settings->tg_channel, 'parse_mode'=>'HTML', "text" => $text_string));
}
}