-
Notifications
You must be signed in to change notification settings - Fork 1
/
cid_twilio.module
171 lines (139 loc) · 4.76 KB
/
cid_twilio.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* @file cid_twilio.module
*
*/
//Define twilio variables
define('CID_TWILIO_DEFAULT_ACCOUNT_SID', '');
define('CID_TWILIO_DEFAULT_AUTH_TOKEN', '');
define('CID_TWILIO_DEFAULT_DEBUG_MODE', 1 );
/*
** Implement hook_menu()
*/
function cid_twilio_menu(){
$items = array();
$items['admin/config/services/cidtwilio'] = array(
'title' => t('CID Twilio API settings'),
'description' => t('Settings for Custom Twilio API'),
'page callback' => 'drupal_get_form',
'page arguments' => array('cid_twilio_admin_form'),
'access arguments' => array('administer cid_twilio'),
'file' => 'cid_twilio.admin.inc',
);
$items['viewtext'] = array(
'title' => t('View CID Twilio Texts'),
'description' => t('View all exisiting CID Twilio Texts'),
'page callback' => 'cid_twilio_execute',
'access arguments' => array('administer cid_twilio'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implement hook_permission()
**/
function cid_twilio_permission(){
return array(
'administer cid_twilio' => array(
'title' => t('Administer CID Twilio'),
),
);
}
/**
* Implement hook_cron()
* Comment out to stop cron run update of Text Messages
**/
function cid_twilio_cron(){
watchdog('cid twilio', 'Twilio Refresh Begin');
cid_twilio_execute();
//watchdog('cid twilio', 'Twilio Refresh Complete');
}
/***
* HTTP Request
**/
function cid_twilio_execute(){
// this loads the library
require('sites/all/libraries/twilio/Services/Twilio.php');
//get account information from variables
$account_sid = variable_get('cid_twilio_account_sid', CID_TWILIO_DEFAULT_ACCOUNT_SID);
$auth_token = variable_get('cid_twilio_auth_token', CID_TWILIO_DEFAULT_AUTH_TOKEN);
$client = new Services_Twilio($account_sid, $auth_token);
$messages = $client->account->messages->getIterator(0, 50, array(
));
//build the query to check if SID already exists in the database
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'text_message');
$result = $efq->execute();
$options = array();
if (isset($result['node'])) {
foreach (node_load_multiple(array_keys($result['node'])) as $nid => $node) {
$options[$nid] = $node->field_sms_id['und'][0]['value'];
}
}
//for dev
//dpm($options);
foreach($messages as $message){
$text_body = '';
$text_date = '';
$text_sms = '';
$text_status = '';
$text_body = check_plain($message->body);
$text_date = $message->date_sent;
$text_incoming = $message->direction;
$text_sms = check_plain($message->sid);
if ((in_array($text_sms, $options)) && ($text_incoming == "inbound")){
$text_status = 'Not Created';
//for dev
//dpm('Node exists, not created');
}
if ((!in_array($text_sms, $options)) && ($text_incoming == "inbound")){
$node = new stdClass();
$node->type = 'text_message';
$node->title = $text_body;
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
$node->field_text_message[$node->language][0]['value'] = $text_body;
$my_date = new DateTime($text_date);
$node->field_date_sent[$node->language][0] = array(
'value' => date_format($my_date, "Y-m-d H:i:s"),
'timezone' => 'UTC',
'timezone_db' => 'UTC',
);
$node->field_sms_id[$node->language][0]['value'] = $text_sms;
global $user;
$node->uid = $user->uid;
$node->name = $user->name;
$node->format = 3; // 1 means filtered html, 2 means full html, 3 is php
$node->status = 1; // 1 means published
$node->promote = 0; // 1 means promoted to front page
$node = node_submit( $node );
//$node = node_validate($form_state['values'], $form);
//for dev
//dpm('New node successfully created');
node_save( $node );
}
}
/*if($node = node_save($node)){
//comment or alter to troubleshoot functions
}
if($text_status = 'Not Created'){
//comment or alter to troubleshoot functions
}
*/
watchdog('cid twilio', 'cid twilio execute function complete');
}
/**
* Implementation of hook_menu_alter().
* Remember to clear the menu cache after adding/editing this function.
* Removing to remove any issues/conflict with UI in editing mode for text_message cck
*/
function cid_twilio_menu_alter(&$items) {
$items['node/%node/view']['access callback'] = 'cid_twilio_disable_node_view';
$items['node/%node/view']['access arguments'] = array(1);
}
function cid_twilio_disable_node_view($node){
if($node->type == 'text_message'){
return false;
}
}