-
Notifications
You must be signed in to change notification settings - Fork 7
/
presentation_workshop_telegram.Rmd
370 lines (262 loc) · 8.25 KB
/
presentation_workshop_telegram.Rmd
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
364
365
366
367
368
369
370
---
title: "Collecting and Analyzing Social Media Data | Telegram Data <br>"
author: "Tiago Ventura | Center for Social Media and Politics | NYU"
date: " <span style = 'font-size: 70%;'> Big Data for Development and Governance <br> 10/21/2022 </span>"
output:
xaringan::moon_reader:
css: ["xaringan-themer.css", "custom.css"]
lib_dir: libs
self_contained: false
nature:
#ratio: 16:9
#ratio: 191:100
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
titleSlideClass: ["center", "middle"]
slideNumberFormat: |
<div class="progress-bar-container">
</div>
<div class="progress-bar" style="width: calc(%current% / %total% * 100%);">
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE, echo=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(messagwese=FALSE, warning = FALSE)
xaringanthemer::style_mono_light(base_color ="#23395b",
title_slide_text_color="#23395b",
title_slide_background_color = "#fff",
background_color = "#fff",
link_color = "#C93312")
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(message=FALSE, warning = FALSE, error=TRUE, echo=TRUE, cache=TRUE)
```
```{r style-share-again, echo=FALSE}
xaringanExtra::use_tile_view()
xaringanExtra::use_panelset()
#xaringanExtra::style_share_again(
# share_buttons = c("twitter", "linkedin", "pocket")
#)
```
class:middle
## Introduction
--
- This notebook walks through some code in Python and R to download and clean data from Telegram.
--
- Telegram has become a very important social media messaging app, particularly in the Global South, as an alternative to WhatsApp.
--
- To capture Telegram data, we will use the Python library [telethon](https://docs.telethon.dev/en/stable/index.html). This library provides an access to telegram API, from which you can grab information from channels using your account.
--
---
## Get your Telegram API credentials
To connect to Telegram, we need an `api_id` and an `api_hash`.
--
- Login to your [Telegram core](https://my.telegram.org/)
--
- Go to the [API development tools area](https://my.telegram.org/apps), and fill a request.
--
- Here’s short [tutorial](https://core.telegram.org/api/obtaining_api_id) about how to get your API credentials.
--
---
## Installing Telethon
```{python eval=FALSE}
#pip3 install telethon
```
---
## APIs Keys
Now, we will load our keys
```{python}
# call some libraries
import os
import datetime
import pandas as pd
from dotenv import load_dotenv
# get the keys
# load keys from environmental var
load_dotenv() # .env file in cwd
telegram_id= os.environ.get("telegram_id")
telegram_hash= os.environ.get("telegram_hash")
# also need your cellphone and username from telegram
phone=os.environ.get("phone_number")
username= os.environ.get("username")
```
---
## Hidden Curriculum: What is the env file?
.center[
```{r echo=FALSE, out.width = "80%"}
knitr::include_graphics("./figs/env.png")
```
]
---
## Log in to Telegram
Now everything is set up, we need to create a client and log in to our telegram account
```{python eval=FALSE}
# call packages
from telethon import TelegramClient #<<
from telethon.errors import SessionPasswordNeededError #<<
from telethon import sync #<<
# Create the client and connect
def telegram_start(username, api_id, api_hash):
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
return client
# Tun the function
client = telegram_start(username, telegram_id, telegram_hash)
```
---
## Log in to Telegram
```{python eval=FALSE}
# call packages
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon import sync
# Create the client and connect
def telegram_start(username, api_id, api_hash): #<<
client = TelegramClient(username, api_id, api_hash) #<<
client.start() #<<
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
return client
# Tun the function
client = telegram_start(username, telegram_id, telegram_hash)
```
---
## Log in to Telegram
```{python eval=FALSE}
# call packages
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon import sync
# Create the client and connect
def telegram_start(username, api_id, api_hash):
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, input('Enter the code: '))
except SessionPasswordNeededError:
client.sign_in(password=input('Password: '))
return client
# Tun the function
client = telegram_start(username, telegram_id, telegram_hash) #<<
```
---
### Getting Channel Members
```{python eval=FALSE}
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.types import (PeerChannel)
# Let's get members of the Lula Channel on Telegram
input_channel = "https://t.me/UrnasEletronicaseEleicoesBrasil" #<<
## Getting information from channel
my_channel = client.get_entity(input_channel) #<<
```
---
### Getting Channel Members
```{python eval=FALSE}
## get channel members
offset = 0
limit = 500
all_participants = []
while True:
participants = client(GetParticipantsRequest( #<<
my_channel, ChannelParticipantsSearch(''), offset, limit, #<<
hash=0 #<<
))
if not participants.users:
break
all_participants.extend(participants.users)
offset += len(participants.users)
```
---
### Cleaning channel members
```{python eval=FALSE}
# Open Json
all_user_details = []
for participant in all_participants:
all_user_details.append(
{"id": participant.id, "first_name": participant.first_name, "last_name": participant.last_name,
"user": participant.username, "phone": participant.phone, "is_bot": participant.bot})
# Check it our
df = pd.DataFrame(all_user_details)
```
---
### Getting Channel Members
.
```{python}
import pandas as pd
df = pd.read_csv("data_telegram/user.csv",)
df.keys()
df.head()
```
---
### Getting Messages
```{python eval=FALSE}
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (PeerChannel)
offset_id = 0
limit = 1000
all_messages = []
total_messages = 0
total_count_limit = 0
# capture data
history = client(GetHistoryRequest( #<<
peer=my_channel,offset_id=offset_id, #<<
offset_date=None,add_offset=0, #<<
limit=limit,max_id=0,min_id=0,hash=0)) #<<
# get messages objects
messages = history.messages
# convert to a dictionary
for message in messages:
all_messages.append(message.to_dict())
# save json
with open('data_telegram/message_data.json', 'w') as outfile:
json.dump(all_messages, outfile, indent=4, sort_keys=True, default=str)
```
---
### Quick data cleaning
```{python}
import pandas as pd
import json
# convert to pandas
# Opening JSON file
f = open('data_telegram/message_data.json')
# returns JSON object as
# a dictionary
data = json.load(f)
df = pd.DataFrame(data)
df.keys()
```
---
### Quick data cleaning
```{python}
# open nested lists
df = pd.concat([df, df["from_id"].apply(pd.Series)], axis=1) #<<
# See
df.head()
```
---
class:middle
### Conclusion
This was a very introduction introduction. If you want to do this at scale, you need to
- Curate a list of channels you are interested in.
- Host this code in a server so that you can make multiple calls over the days.
- Use the async package to make this code more efficient.