-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegramStorage.php
More file actions
82 lines (71 loc) · 2.02 KB
/
telegramStorage.php
File metadata and controls
82 lines (71 loc) · 2.02 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
<?php
include('vendor/api/autoload.php'); //Подключаем библиотеку
use Telegram\Bot\Api;
/*
* Input: file_name
* Output: file_content
*/
Class TelegramStorage {
private $telegram = null;
private $chat_id = '@my_activity';
private $token = null;
function __construct($token = null) {
$this->token = $token;
$this->telegram = new Api($this->token); //Устанавливаем токен, полученный у BotFather
}
public function getFile($localpath) {
$file_name = $this->prepareFileName($localpath);
$file_id = $this->getFileId($file_name);
if($file_id) {
$answer = $this->telegram->getFile(['chat_id' => $this->chat_id, 'file_id' => $file_id]);
$url = 'https://api.telegram.org/file/bot' . $this->token . '/' . $answer['file_path'];
return $url;
} else {
return false;
}
}
public function saveFile($localpath = '') {
$file_name = $this->prepareFileName($localpath);
$key = md5($file_name);
$tData = $this->telegram->sendDocument([ 'chat_id' => $this->chat_id, 'document' => $localpath ]);
$file_id = $tData['document']['file_id'];
$this->memcachedSet($key,$file_id);
return $file_id;
}
private function getFileId($file_name) {
$key = md5($file_name);
echo 'get key: ' . $key . "\r\n";
$a = $this->memcachedGet($key);
return $a;
}
private function memcachedSet($key,$val) {
$mem_var = $this->createMemcached_server();
echo 'set key: ' . $key . "\r\n";
$response = $mem_var->set($key,$val);
if ($response) {
return $response;
} else {
return false;
}
}
private function memcachedGet($key) {
$mem_var = $this->createMemcached_server();
$response = $mem_var->get($key);
if ($response) {
return $response;
} else {
return false;
}
}
private function createMemcached_server(){
$mem_var = new Memcached();
$mem_var->addServer("127.0.0.1", 11211);
return $mem_var;
}
private function prepareFileName($localpath) {
$match = explode('/',$localpath);
$file_name = $match[ sizeof( $match ) - 1 ];
return $file_name;
}
}
?>