-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.php
152 lines (143 loc) · 8.3 KB
/
bot.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require "./vendor/autoload.php";
use Jajo\JSONDB;
$Bot = new Boting\Boting();
if (file_exists(__DIR__ . '/.env')) {
$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();
}
if (file_exists(__DIR__ . '/language/default.json')) {
$LANG = json_decode(file_get_contents("./language/default.json"), true);
} else {
$LANG = json_decode('{
"START": "Hi, You can message my owner with this bot. You can send any media. My owner will reply ur message ASAP!.",
"ERROR_REPLY": "*Please reply an message.*",
"ERROR_NOTFOUND": "*Sorry! Message not found in the database and infact could not able to reply.*",
"SENDER": "*Sender:*"
}', true);
}
if (empty(getenv("OWNER_ID"))) {
echo "Please give owner id";
$Owner = 9876543210;
} else {
$Owner = getenv("OWNER_ID");
}
if (empty(getenv("DB_TUR"))) {
$tur = 0;
$db = new JSONDB(__DIR__);
} else {
if (getenv("DB_TUR") == "json") {
$tur = 0;
$db = new JSONDB( __DIR__ );
} elseif (getenv("DB_TUR") == "db") {
$tur = 1;
if (empty(getenv("DATABASE_URL"))) {
$tur = 0;
$db = new JSONDB( __DIR__ );
return;
} else {
$url = parse_url($_ENV["DATABASE_URL"]);
}
try {
$db = new PDO("pgsql:host=" . $url["host"] . ";port=" . $url["port"] . ";dbname=" . ltrim($url["path"], "/"), $url["user"], $url["pass"]);
$db->exec("CREATE TABLE IF NOT EXISTS msg(
mid integer,
fid integer,
fmid integer,
fname text,
fusername text,
time integer);");
} catch ( PDOException $e ){
print $e->getMessage();
}
}
}
foreach (glob("./commands/*.php") as $file) {
include($file);
}
$Bot->on(["animation", "audio", "document", "photo", "sticker", "video", "video_note", "voice", "contact", "dice", "location", "text"], function ($Update) use ($Bot, $Owner, $db, $tur, $LANG) {
if ($Update["message"]["chat"]["type"] !== "private") {
return;
}
if ($Update["message"]["chat"]["id"] == $Owner) {
if(empty($Update["message"]["reply_to_message"])) {
$Bot->sendMessage(["chat_id" => $Update["message"]["chat"]["id"], "reply_to_message_id" => $Update["message"]["message_id"], "parse_mode" => "markdown", "text" => $LANG["ERROR_REPLY"]]);
return;
} else {
$MId = $Update["message"]["reply_to_message"]["message_id"];
if ($tur == 0) {
$fid = $db->select('fid, fmid')
->from('msg.json')
->where(['mid' => $MId])
->get()[0];
} else {
$query = $db->prepare("SELECT fid, fmid FROM msg WHERE mid = " . $MId);
$query->execute();
$fid = $query->fetch();
}
if (empty($fid)) {
$Bot->sendMessage(["chat_id" => $Update["message"]["chat"]["id"], "reply_to_message_id" => $Update["message"]["message_id"], "parse_mode" => "markdown", "text" => $LANG["ERROR_NOTFOUND"]]);
return;
}
if (!empty($Update["message"]["animation"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "animation" => $Update["message"]["animation"]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendAnimation($Array);
} elseif (!empty($Update["message"]["audio"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "audio" => $Update["message"]["audio"]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendAudio($Array);
} elseif (!empty($Update["message"]["document"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "document" => $Update["message"]["document"]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendDocument($Array);
} elseif (!empty($Update["message"]["photo"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "photo" => array_reverse($Update["message"]["photo"])[0]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendPhoto($Array);
} elseif (!empty($Update["message"]["sticker"])) {
$Bot->sendSticker(["chat_id" => $fid["fid"], "reply_to_message_id" => $fid["fmid"], "sticker" => $Update["message"]["sticker"]["file_id"]]);
} elseif (!empty($Update["message"]["video"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "video" => $Update["message"]["video"]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendVideo($Array);
} elseif (!empty($Update["message"]["video_note"])) {
$Bot->sendVideoNote(["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "video_note" => $Update["message"]["video_note"]["file_id"]]);
} elseif (!empty($Update["message"]["voice"])) {
$Array = ["chat_id" => $fid["fid"], "parse_mode" => "markdown", "reply_to_message_id" => $fid["fmid"], "video_note" => $Update["message"]["voice"]["file_id"]];
if (!empty($Update["message"]["caption"])) $Array["caption"] = $Update["message"]["caption"];
$Bot->sendVoice($Array);
} elseif (!empty($Update["message"]["contact"])) {
$Bot->sendContact(["chat_id" => $fid["fid"], "reply_to_message_id" => $fid["fmid"], "phone_number" => $Update["message"]["contact"]["phone_number"], "first_name" => $Update["message"]["contact"]["first_name"]]);
} elseif (!empty($Update["message"]["dice"])) {
$Bot->sendDice(["chat_id" => $fid["fid"], "reply_to_message_id" => $fid["fmid"], "dice" => $Update["message"]["dice"]["emoji"]]);
} elseif (!empty($Update["message"]["location"])) {
$Bot->sendLocation(["chat_id" => $fid["fid"], "reply_to_message_id" => $fid["fmid"], "latitude" => $Update["message"]["location"]["latitude"], "longitude" => $Update["message"]["location"]["longitude"]]);
} elseif (!empty($Update["message"]["text"])) {
$Bot->sendMessage(["chat_id" => $fid["fid"], "reply_to_message_id" => $fid["fmid"], "text" => $Update["message"]["text"],"parse_mode" => "markdown",]);
}
}
} else {
$id = $Bot->forwardMessage(["chat_id" => $Owner, "from_chat_id" => $Update["message"]["chat"]["id"], "message_id" => $Update["message"]["message_id"]])["result"]["message_id"];
if (!empty($Update["message"]["sticker"])) {
$Bot->sendMessage(["chat_id" => $Owner, "parse_mode" => "markdown", "reply_to_message_id" => $id, "text" => $LANG["sender"] . " [" . $Update["message"]["from"]["first_name"] . "](tg://user?id=" . $Update["message"]["chat"]["id"] . ")"]);
}
if (!empty($Update["message"]["from"]["username"])) {
$username = $Update["message"]["from"]["username"];
} else {
$username = "No";
}
if ($tur == 0) {
$db->insert('msg.json', ["mid" => $id, "fmid" => $Update["message"]["message_id"], "fid" => $Update["message"]["from"]["id"], "fname" => $Update["message"]["from"]["first_name"], "fusername" => $username, "time" => time()]);
} else {
$db->prepare("INSERT INTO msg (mid, fmid, fid, fname, fusername, time) VALUES (?,?,?,?,?,?)")->execute([$id, $Update["message"]["message_id"], $Update["message"]["from"]["id"], $Update["message"]["from"]["first_name"], $username, time()]);
}
}
});
if (empty(getenv("BOT_TOKEN"))) {
echo "Please add token";
die();
} else {
echo "bot started";
$Bot->Handler(getenv("BOT_TOKEN"));
}