-
Notifications
You must be signed in to change notification settings - Fork 26
/
telegram.module
133 lines (124 loc) · 3.55 KB
/
telegram.module
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
<?php
/**
* @file
* Drupal module: Telegram
*
* Experimental development, guess what...
*
* By Jose Manuel Guerrero & Jose Reyero
*
* http://reyero.net
* http://jmanuelguerrero.com
*/
use Drupal\telegram\TelegramLogger;
use Drupal\telegram\TelegramProcess;
use Drupal\telegram\DrupalTelegramClient;
/**
* Implements hook_menu().
*/
function telegram_menu() {
$items['admin/config/telegram'] = array(
'title' => 'Telegram',
'access arguments' => array('administer site configuration'),
'position' => 'right',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/telegram/settings'] = array(
'title' => 'Settings',
'description' => 'Configure Telegram',
'page callback' => 'drupal_get_form',
'page arguments' => array('variable_module_form', 'telegram'),
'access arguments' => array('administer site configuration'),
);
$items['admin/config/telegram/test'] = array(
'title' => 'Test',
'description' => 'Test configuration settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('telegram_test_form'),
'access arguments' => array('administer site configuration'),
'file' => 'telegram.admin.inc',
);
return $items;
}
/**
* Send message to phone number.
*/
function telegram_send_phone($phone, $text) {
if ($contact = telegram_instance()->getContactByPhone($phone)) {
return telegram_send_peer($contact->peer, $text);
}
else {
drupal_set_message(t('Cannot find contact for phone @number', array('@number' => $phone)), 'warning');
return FALSE;
}
}
/**
* Send message to peer, shorthand API function.
*
* @param string $peer
* Peer name.
* @param string $text
* Message text.
*/
function telegram_send_peer($peer, $text) {
if (telegram_instance()->sendMessage($peer, $text)) {
drupal_set_message(t('Telegram message sent successfully.'));
return TRUE;
}
else {
drupal_set_message(t('Error sending Telegram message'));
return FALSE;
}
}
/**
* Get telegram instance.
*
* This one will return client or manager depending on the enabled modules.
*
* @return Drupal\telegram\TelegramInterface
*/
function telegram_instance() {
return function_exists('telegram_manager') ? telegram_manager() : telegram_client();
}
/**
* Get Telegram client
*
* @return Drupal\telegram\TelegramClient
*/
function telegram_client() {
$telegram = &drupal_static('telegram');
if (!isset($telegram['client'])) {
$logger = telegram_logger();
$process = new TelegramProcess(_telegram_params(), $logger);
$telegram['client'] = new DrupalTelegramClient($process, $logger);
}
return $telegram['client'];
}
/**
* Get Telegram logger.
*
* @return Drupal\telegram\TelegramLogger
*/
function telegram_logger() {
$telegram = &drupal_static('telegram');
if (!isset($telegram['logger'])) {
$telegram['logger'] = new TelegramLogger(_telegram_params());
}
return $telegram['logger'];
}
/**
* Get configuration as array.
*/
function _telegram_params() {
return array(
'command' => variable_get_value('telegram_command_exec'),
'keyfile' => variable_get_value('telegram_command_key'),
'homepath' => variable_get_value('telegram_command_cwd'),
'configfile' => variable_get_value('telegram_config_path'),
'log_level' => variable_get_value('telegram_command_debug') ? 0 : 1,
'log_file' => variable_get_value('telegram_log_file'),
);
}