-
-
Notifications
You must be signed in to change notification settings - Fork 155
/
im.php
91 lines (84 loc) · 2.34 KB
/
im.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
<?php
namespace Bitrix24\Im;
use Bitrix24\Bitrix24Entity;
use Bitrix24\Bitrix24Exception;
use Bitrix24\Presets\Im\Fields as B24ImFields;
/**
* Class Im
* @package Bitrix24\Im
*/
class Im extends Bitrix24Entity
{
/**
* send notification to user
* @link https://training.bitrix24.com/rest_help/im/im_notify.php
* @param $userId integer bitrix24 user identifier
* @param $message string message to user, support some html tags
* @param $notifyType string
* - SYSTEM send message from current application
* - USER send message from current user, default value
* @return array
* @throws Bitrix24Exception
*/
public function notify($userId, $message, $notifyType = B24ImFields::NOTIFY_TYPE_USER)
{
if(is_null($userId))
{
throw new Bitrix24Exception('user id is null');
}
elseif(is_null($message))
{
throw new Bitrix24Exception('message is null');
}
elseif(!in_array(strtoupper($notifyType), array(B24ImFields::NOTIFY_TYPE_SYSTEM, B24ImFields::NOTIFY_TYPE_USER), true))
{
throw new Bitrix24Exception('unknown notifyType');
}
$fullResult = $this->client->call(
'im.notify',
array(
'to' => $userId,
'message' => $message,
'type' => strtoupper($notifyType)
)
);
return $fullResult;
}
/**
* add a message to private/public group chats
* @param int $chatId
* @param $message
* @param string $system
* @param string $userId
* @return array
*/
public function messageAdd($chatId = 1, $message, $system = 'N', $userId = '')
{
if(is_null($chatId) && empty($userId))
{
throw new Bitrix24Exception('chat id is null and user id is empty');
}
elseif(is_null($message))
{
throw new Bitrix24Exception('message is null');
}
elseif(!in_array($system, array("N", "Y"), true))
{
throw new Bitrix24Exception('unknown system');
}
$arAdditionalParameters = array(
'message' => $message,
'system' => $system
);
if (!empty($userId)) {
$arAdditionalParameters['user_id'] = $userId;
} else {
$arAdditionalParameters['chat_id'] = $chatId;
}
$fullResult = $this->client->call(
'im.message.add',
$arAdditionalParameters
);
return $fullResult;
}
}