forked from Surzhikov/TelegramSiteHelper
/
sendMessage.php
91 lines (52 loc) · 2.05 KB
/
sendMessage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?
$start=time();
// Connect to database
require('TelegramDBConnect.php');
$db=connectDB();
// Create new Bot Object
require('TelegramBotClass.php');
require('TelegramBotConfig.php');
$bot = new TelegramBot($tokenAPI);
// Message & chatId from web-site
$wsMessage=$_POST['wsMessage'];
if($_COOKIE['wsChatHash']!=null){
$wsChatHash=$_COOKIE['wsChatHash'];
$sth=$db->prepare("SELECT wsChatId FROM wsChat WHERE wsChatHash=:wsChatHash");
$sth->execute(array(":wsChatHash"=>$wsChatHash));
$a=$sth->fetch();
$wsChatId=$a['wsChatId'];
}else{
$sth=$db->prepare("SELECT MAX(wsChatId) as max FROM wsChat");
$sth->execute();
$a=$sth->fetch();
$wsChatId=intval($a['max'])+1;
$wsChatHash=uniqid();
$sth=$db->prepare("INSERT INTO wsChat (wsChatId, wsChatHash) VALUES (:wsChatId, :wsChatHash)");
$sth->execute(array(":wsChatId"=>$wsChatId, ":wsChatHash"=>$wsChatHash));
setcookie("wsChatHash", $wsChatHash, time()+(60*60*24));
}
$msg=$wsMessage."\r\n»» Для перехода в чат нажмите /chat_".$wsChatId;
$sth=$db->prepare("SELECT chatId FROM managers WHERE wsChat=:wsChat");
$sth->execute(array(":wsChat"=>$wsChatId));
$a=$sth->fetch();
if($a['chatId']==null){
$sth=$db->prepare("SELECT chatId FROM managers");
$sth->execute();
$managerChat=array();
while($a=$sth->fetch()){
$managerChat[]=$a['chatId'];
}
$randomManagerChat=$managerChat[rand(0,count($managerChat)-1)];
$sth=$db->prepare("UPDATE managers SET wsChat=:wsChat WHERE chatId=:chatId");
$sth->execute(array(":wsChat"=>$wsChatId,":chatId"=>$randomManagerChat));
$msgSent=$bot->SendMessage($randomManagerChat,$msg);
}else{
$msgSent=$bot->SendMessage($a['chatId'],$msg);
}
$dbname="chatsDBs/".$wsChatHash.".db";
// Connect to database
require('chatDBConnect.php');
$db=chatDBConnect($dbname);
$sth=$db->prepare("INSERT INTO messages (mText, mCreator, mDate) VALUES (:mText, :mCreator, :mDate)");
$sth->execute(array(":mText"=>$wsMessage, ":mCreator"=>"c", ":mDate"=>time()));
echo json_encode(array("status"=>"ok", "wsChatHash"=>$wsChatHash));