-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsendphoto.php
More file actions
105 lines (104 loc) · 3.39 KB
/
sendphoto.php
File metadata and controls
105 lines (104 loc) · 3.39 KB
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
<?php
$TOKEN = 'BOT TOKEN';
function request_url($method)
{
global $TOKEN;
return "https://api.telegram.org/bot" . $TOKEN . "/". $method;
}
function get_updates($offset)
{
$url = request_url("getUpdates")."?offset=".$offset;
$resp = file_get_contents($url);
$result = json_decode($resp, true);
if ($result["ok"] == true) {
return $result["result"];
}
return array();
}
function send_reply($chatid, $msgid, $text)
{
$data = array(
'chat_id' => $chatid,
'text' => $text,
'reply_to_message_id' => $msgid
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents(request_url('sendMessage'), false, $context);
print_r($result);
}
function sendPhoto($chatid, $urlphoto, $caption)
{
$url = request_url('sendPhoto');
$content = array( 'chat_id' => $chatid, 'photo' => new CURLFile(realpath($urlphoto)), 'caption' => $caption);
print_r($content);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status != 201) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
return $response;
}
function create_response($text)
{
return "definisi " . $text;
}
function process_message($message)
{
$updateid = $message["update_id"];
$message_data = $message["message"];
if (isset($message_data["text"])) {
$chatid = $message_data["chat"]["id"];
$message_id = $message_data["message_id"];
$text = $message_data["text"];
$response = create_response($text);
send_reply($chatid, $message_id, $response);
}
return $updateid;
}
function process_one()
{
$update_id = 0;
if (file_exists("last_update_id")) {
$update_id = (int)file_get_contents("last_update_id");
}
$updates = get_updates($update_id);
foreach ($updates as $message) {
$update_id = process_message($message);
}
file_put_contents("last_update_id", $update_id + 1);
}
function searchInMessage($keyword, $urlPhoto, $caption)
{
$updates = get_updates(0);
foreach ($updates as $message) {
$message_data = $message["message"];
if ($message_data['text'] == $keyword) {
echo 'chat_id : '.$message_data['chat']['id'].'<br>';
echo 'from : '.$message_data['from']['username'].'<br>';
echo 'isi : '.$message_data['text'].'<br>';
sendPhoto($message_data['chat']['id'], $urlPhoto, $caption);
}
}
}
searchInMessage('Linephoto', 'testphoto.jpg', 'Ini Udah bisa');
//while (true) {
//process_one();
//}
//echo get_updates(0);