-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.php
150 lines (126 loc) · 5.07 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
<?php
$path = "https://api.telegram.org/bot<Your Bot Token>/";
$update = json_decode(file_get_contents("php://input"), TRUE);
$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$first = $update["message"]["from"]["first_name"];
$last = $update["message"]["from"]["last_name"];
$callback_query = $update['callback_query'];
//***********************************************
$buttons = json_encode(
[ 'keyboard' =>
[
['📁 Upload Photo'],
],
'resize_keyboard'=>true,
]);
//************************ START MAIN ****************************
if($message=="/start"){
$msg=
array(
'chat_id' => $chatId,
'text' =>"Welcome, ".$first
."\nHoran OCR - free Text from Image Scanner \n",
'parse_mode' => 'HTML',
'reply_markup' =>$buttons,
'disable_web_page_preview' => false);
//'reply_markup' =>$print,
send("sendMessage", $msg);
}
//************************ START MAIN ****************************
if($message=="📁 Upload Photo"){
$msg=
array(
'chat_id' => $chatId,
'text' =>"✅Crop part of image you want to translate and send here.
NB: ● Only Image with English Text Supported.
● Accuracy is low for large text or low quality images",
'parse_mode' => 'HTML',
'disable_web_page_preview' => false);
send("sendMessage", $msg);
}
//************************ TEXT LISTNER ****************************
if($message !="/start" and $message !="")
{
$msg2=
array(
'chat_id' => $chatId,
'text' =>"Ok, now send the image to scan...",
'parse_mode' => 'HTML',
'disable_web_page_preview' => false);
send("sendMessage", $msg2);
}
//************************ UPLOAD IMAGE ****************************
if($update['message']['photo'][1]['file_id'])
{
$file_id=$update['message']['photo'][1]['file_id'];
$geturl=$path."getFile?file_id=".$file_id;
$file_path_json=file_get_contents($geturl);
$file_path_json=json_decode($file_path_json, true);
$file_path=$file_path_json['result']['file_path'];
$filepath="https://api.telegram.org/file/bot<Your Bot Token>/".$file_path;
// Initialize a file URL to the variable
$url = $filepath;
$file_name = basename($url);
if (file_put_contents('images/'.$file_name, file_get_contents($url)))
{
// Change url to place you want to store uploaded images
$id_url= 'https://www.yourdomain.com/path/img-folder/'.$file_name;
echo "File downloaded successfully";
$wait=
array(
'chat_id' => $chatId,
'text' =>"Processing, please wait....",
'parse_mode' => 'HTML');
send("sendMessage", $wait);
}
else
{
$wait=
array(
'chat_id' => $chatId,
'text' =>"Server Error, Downloading faild Try again....",
'parse_mode' => 'HTML',
'disable_web_page_preview' => false);
send("sendMessage", $wait);
}
//Change OCR-KEY to your api key from ocr.space website
$result = file_get_contents('http://api.ocr.space/parse/imageurl?apikey=<OCR-KEY-XXX>&url='.$id_url);
$result=json_decode($result, true);
$str='';
foreach($result['ParsedResults'] as $pareValue)
{
$str.= strval($pareValue['ParsedText']);
}
if($str!=""){
$re=
array(
'chat_id' => $chatId,
'text' => "🔍Scanned Text:\n$str\n\n@horansoftware",
'disable_web_page_preview' => false,);
send("sendMessage", $re);
}
else{
$error=
array(
'chat_id' => $chatId,
'text' =>"Server Error, Scanning faild Try again...\nNB: only Image with English Text Supported",
'parse_mode' => 'HTML',
'disable_web_page_preview' => false);
send("sendMessage", $error);
}
}
function send($method, $data)
{
$url = "https://api.telegram.org/bot<Your Bot Token>/".$method;
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
return $output;
}