-
Notifications
You must be signed in to change notification settings - Fork 8
/
Twitter-Integration
363 lines (319 loc) · 11.9 KB
/
Twitter-Integration
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* Class Name: TwitterController
* Description: Class for calling Twitter API with accountId getting on page load.
* --------
**/
global with sharing class TwitterController
{
private ApexPages.StandardController TwitterController ;
public String sBody{get;set;}
public string returnValue;
public static String tweets{get;set;}
public static String access_token{get;set;}
public Account acc{get;set;}
public string AccountId{get;set;}
public Account acc1=new Account();
public list<Account>listacc=new list<Account>();
public string TwitterUserName{get; set;}
public static string v;
public static String accountName{get;set;}
public TwitterController(ApexPages.StandardController controller)
{
}
public TwitterController()
{
}
public void establishAPIConnection()
{
String keyencoded = EncodingUtil.urlEncode('CYXp886M6g04xwouOHLYg4UJf','UTF-8');
String secretkeyencoded = EncodingUtil.urlEncode('5qC4A3JZDF8you0VtKx95AJqROlXi2oihTkJgVNNsv8pVQ2cjT', 'UTF-8');
String sFinal = keyencoded + ':' + secretkeyencoded;
Blob headerValue = Blob.valueOf(sFinal);
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twitter.com/oauth2/token');
req.setMethod('POST');
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setBody('grant_type=client_credentials');
Http http = new Http();
HTTPResponse res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
while(parser.nextToken() != null)
{
if(parser.getCurrentToken() == JSONToken.FIELD_NAME)
{
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token')
{
access_token = parser.getText();
}
}
}
gettweetsss();
}
/* public TwitterController()
{
//GetAccount con2 = new GetAccount();
//returnValue = con2.getAcc(accountName);
accountName = GetAccount.getAcc();
}*/
public void gettweetsss()
{
HttpRequest req2 = new HttpRequest();
list<Account>accts=new list<Account>();
list<Accounts_Social_Media_Monitoring__c> lstSMM=new list<Accounts_Social_Media_Monitoring__c>();
map<id,Account> map1=new map<id, Account>();
system.debug('Prashant asking accountName' +GetAccount.accountName);
accts=[SELECT Id,Twitter_Id__c, Name FROM Account where Twitter_Id__c!=NULL AND id=:GetAccount.accountName];
for(Account a:accts)
{
Accounts_Social_Media_Monitoring__c objSMM = new Accounts_Social_Media_Monitoring__c();
objSMM.Account_ID__c = a.ID;
objSMM.Social_Media_Name__c = a.Twitter_Id__c;
//ac.Social_Media_Name__c=map1.get(ac.Account_ID__c).Twitter_Id__c;
//list1.add(ac);
string v=a.Twitter_Id__c;
req2.setEndpoint('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='+v);
req2.setMethod('GET');
//Call Bearer token Method
String authorizationHeader1 = 'Bearer ' + access_token;
req2.setHeader('Authorization', authorizationHeader1);
Http http1 = new Http();
HTTPResponse res1 = http1.send(req2);
tweets = res1.getBody();
system.debug('Checking tweets now'+tweets);
String response=res1.getBody();
String text='';
JSONParser parser1 = JSON.createParser(response);
while (parser1.nextToken() != null)
{
if ((parser1.getCurrentToken() == JSONToken.FIELD_NAME))
{
String fieldName = parser1.getText();
parser1.nextToken();
String fieldValue=parser1.getText();
if(fieldName == 'id_str')
{
system.debug('TW ID: ' + parser1.getText());
objSMM.Tweet_id__c = parser1.getText();
parser1.nextToken();
if(parser1.getText()=='text')
{
parser1.nextToken();
text= parser1.getText();
system.debug('TW: ' + text);
objSMM.Tweet_Text__c = parser1.getText();
break;
}
}
}
}
list<Accounts_Social_Media_Monitoring__c> lst = [select ID from Accounts_Social_Media_Monitoring__c where Tweet_id__c = :objSMM.Tweet_id__c];
system.debug('Found records: ' + lst.Size());
if(lst.Size() == 0)
{
system.debug('adding record to list');
lstSMM.add(objSMM);
}
}
system.debug('inserting records: ' + lstSMM.Size());
insert(lstSMM);
}
}
=====================================================================================================================================================
/*Get tweets with Screen name- Prashant Singh*/
public class TwitterController
{
private ApexPages.StandardController TwitterController ;
public String sBody{get;set;}
public String tweets{get;set;}
public String access_token{get;set;}
public Account acc{get;set;}
public TwitterController tc{get; set;}
public AccountId{get;set;}
public Account acc1=new Account();
public list<Account>listacc=new list<Account>();
public string TwitterUserName{get; set;}
public TwitterController(ApexPages.StandardController controller)
{
acc1=[select id, Name from Account where id=:ApexPages.currentPage().getParameters().get('id')];
acc=new Account();
AccountId=acc1.name;
}
public TwitterController(ApexPages.StandardController controller)
{
String keyencoded = EncodingUtil.urlEncode('CYXp886M6g04xwouOHLYg4UJf','UTF-8');
String secretkeyencoded = EncodingUtil.urlEncode('5qC4A3JZDF8you0VtKx95AJqROlXi2oihTkJgVNNsv8pVQ2cjT', 'UTF-8');
String sFinal = keyencoded + ':' + secretkeyencoded;
Blob headerValue = Blob.valueOf(sFinal);
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twitter.com/oauth2/token');
req.setMethod('POST');
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setBody('grant_type=client_credentials');
Http http = new Http();
HTTPResponse res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
while(parser.nextToken() != null)
{
if(parser.getCurrentToken() == JSONToken.FIELD_NAME)
{
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token')
{
access_token = parser.getText();
}
}
}
}
public String gettweetsss()
{
HttpRequest req2 = new HttpRequest();
list<Account>accts=new list<Account>();
list<Accounts_Social_Media_Monitoring__c> lstSMM=new list<Accounts_Social_Media_Monitoring__c>();
map<id,Account> map1=new map<id, Account>();
accts=[SELECT Id,Twitter_Id__c, Name FROM Account where Twitter_Id__c!=NULL];
for(Account a:accts)
{
Accounts_Social_Media_Monitoring__c objSMM = new Accounts_Social_Media_Monitoring__c();
objSMM.Account_ID__c = a.ID;
objSMM.Social_Media_Name__c = a.Twitter_Id__c;
//ac.Social_Media_Name__c=map1.get(ac.Account_ID__c).Twitter_Id__c;
//list1.add(ac);
string v=a.Twitter_Id__c;
req2.setEndpoint('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='+v);
req2.setMethod('GET');
//Call Bearer token Method
String authorizationHeader1 = 'Bearer ' + access_token;
req2.setHeader('Authorization', authorizationHeader1);
Http http1 = new Http();
HTTPResponse res1 = http1.send(req2);
tweets = res1.getBody();
//system.debug(tweets);
String response=res1.getBody();
String text='';
JSONParser parser1 = JSON.createParser(response);
while (parser1.nextToken() != null)
{
if ((parser1.getCurrentToken() == JSONToken.FIELD_NAME))
{
String fieldName = parser1.getText();
parser1.nextToken();
String fieldValue=parser1.getText();
if(fieldName == 'id_str')
{
system.debug('TW ID: ' + parser1.getText());
objSMM.Tweet_id__c = parser1.getText();
parser1.nextToken();
if(parser1.getText()=='text')
{
parser1.nextToken();
text= parser1.getText();
system.debug('TW: ' + text);
objSMM.Tweet_Text__c = parser1.getText();
break;
}
}
}
}
list<Accounts_Social_Media_Monitoring__c> lst = [select ID from Accounts_Social_Media_Monitoring__c where Tweet_id__c = :objSMM.Tweet_id__c];
system.debug('Found records: ' + lst.Size());
if(lst.Size() == 0)
{
system.debug('adding record to list');
lstSMM.add(objSMM);
}
}
system.debug('inserting records: ' + lstSMM.Size());
insert(lstSMM);
}
===================================================&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&==========================================================================
private ApexPages.StandardController TwitterController ;
public String sBody{get;set;}
public String tweets{get;set;}
public String access_token{get;set;}
String keyencoded = EncodingUtil.urlEncode('CYXp886M6g04xwouOHLYg4UJf','UTF-8');
String secretkeyencoded = EncodingUtil.urlEncode('5qC4A3JZDF8you0VtKx95AJqROlXi2oihTkJgVNNsv8pVQ2cjT', 'UTF-8');
String sFinal = keyencoded + ':' + secretkeyencoded;
Blob headerValue = Blob.valueOf(sFinal);
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.twitter.com/oauth2/token');
req.setMethod('POST');
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setBody('grant_type=client_credentials');
Http http = new Http();
HTTPResponse res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
while(parser.nextToken() != null)
{
if(parser.getCurrentToken() == JSONToken.FIELD_NAME)
{
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'access_token')
{
access_token = parser.getText();
}
}
}
HttpRequest req2 = new HttpRequest();
list<Account>accts=new list<Account>();
list<Accounts_Social_Media_Monitoring__c> lstSMM=new list<Accounts_Social_Media_Monitoring__c>();
map<id,Account> map1=new map<id, Account>();
accts=[SELECT Id,Twitter_Id__c, Name FROM Account where Twitter_Id__c!=NULL];
for(Account a:accts)
{
Accounts_Social_Media_Monitoring__c objSMM = new Accounts_Social_Media_Monitoring__c();
objSMM.Account_ID__c = a.ID;
objSMM.Social_Media_Name__c = a.Twitter_Id__c;
//ac.Social_Media_Name__c=map1.get(ac.Account_ID__c).Twitter_Id__c;
//list1.add(ac);
string v=a.Twitter_Id__c;
req2.setEndpoint('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name='+v);
req2.setMethod('GET');
//Call Bearer token Method
String authorizationHeader1 = 'Bearer ' + access_token;
req2.setHeader('Authorization', authorizationHeader1);
Http http1 = new Http();
HTTPResponse res1 = http1.send(req2);
tweets = res1.getBody();
//system.debug(tweets);
String response=res1.getBody();
String text='';
JSONParser parser1 = JSON.createParser(response);
while (parser1.nextToken() != null)
{
if ((parser1.getCurrentToken() == JSONToken.FIELD_NAME))
{
String fieldName = parser1.getText();
parser1.nextToken();
String fieldValue=parser1.getText();
if(fieldName == 'id_str')
{
system.debug('TW ID: ' + parser1.getText());
objSMM.User_id__c = parser1.getText();
parser1.nextToken();
if(parser1.getText()=='text')
{
parser1.nextToken();
text= parser1.getText();
system.debug('TW: ' + text);
objSMM.Tweet_Text__c = parser1.getText();
break;
}
}
}
list<Accounts_Social_Media_Monitoring__c> lst = [select ID from Accounts_Social_Media_Monitoring__c where User_id__c = :objSMM.User_id__c];
if(lst.Size() == 0)
{
//system.debug('This record will not be inserted');
lstSMM.add(objSMM);
}
}
}
system.debug('inserting records: ' + lstSMM.Size());
//insert(lstSMM);