-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelegram.R
152 lines (108 loc) · 5.19 KB
/
Telegram.R
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
## https://github.com/lbraglia/telegram
# install.packages('telegram')
# Load Libraries ----------------------------------------------------------
library(telegram)
library(httr)
library(dplyr)
library(tidyr)
library(tibble)
library(glue)
# Tokens ------------------------------------------------------------------
Notion_secret <- Sys.getenv("NOTION_SECRET")
Notion_database <- Sys.getenv("NOTION_DATABASE")
Telegram_token <- Sys.getenv("R_TELEGRAM_BOT_RBot")
Telegram_chatID <- Sys.getenv("R_TELEGRAM_USER_me")
# Pull 'Quotes' DB from Notion --------------------------------------------
getNotionDatabase <- function(secret, database, filters = NULL, show_progress = FALSE, all_pages = TRUE, cover_icon = FALSE){
options(dplyr.summarise.inform = FALSE) # to supress all the grouping warnings!
# +++++++++ construct headers
headers = c(`Authorization` = secret, `Notion-Version` = '2022-02-22', `Content-Type` = 'application/json' )
# +++++++++ api call -------------------------------------------------------------
callAPI <- function(database, headers, filters, cursor){
res <- httr::POST(url = paste0('https://api.notion.com/v1/databases/', database, '/query'),
httr::add_headers(.headers = headers),
body = list("filters" = filters,
"start_cursor" = cursor),
# start_cursor = cursor,
encode = "json")
if(show_progress){ print(paste0("!! API Call: https://api.notion.com/v1/databases/", database, "/query")) }
return( httr::content(res) )
}
# +++++++++ this function "flattens" the results into a usable data.frame with 1 row per page (like the real database)
getItemsAndFlattenIntoDataFrame <- function(results, cover_and_icon = cover_icon){
if(show_progress){ print(paste0("- flattening into data.frame")) }
# the results (i.e., rows) are extracted into a simple data.frame with value being a list of each item's properties and id's
items <- tibble::enframe(results)
# now, for each item, we will extract a tidy data.frame where we have all of the columns
dd <- NULL
for(i in 1:nrow(items)){
## before we tidy up,
## add NA's if there is no cover or icon AND we want to based on the option in the parameters of the function
if(cover_and_icon){
if(is.null( items[[2]][[i]][["cover"]] )){
items[[2]][[i]][["cover"]] <- as.logical("FALSE")
}
if(is.null( items[[2]][[i]][["icon"]] )){
items[[2]][[i]][["icon"]] <- as.logical("FALSE")
}
}
# this is a tidy dataset with column 1 = name (i.e., value.object.type, etc) and col2 = value (i.e,. d3f0ee76-fc3b-426c-8d23-cff84800b0d6)
tmp <- tibble::enframe(unlist(items[[i, 2]]))
# to avoid duplicates, (such as two relationships tied to a page) I will condense them into 1 separated by a pipe
tmp <- tmp %>%
group_by(name) %>%
summarise("value" = paste(value, collapse = " | "))
# now, I want to keep this as 1 row in a big data set, so I will pivot_wider
tmp <- tidyr::pivot_wider(tmp)
# now, I will create one big dataset, I will use dplyr in case columns are not exactly the same, which could be the case if one or various of the properties are missing
dd <- dplyr::bind_rows(dd, tmp)
}
return(dd)
}
if(all_pages){
if(show_progress){ print(paste0("++++ PAGINATING CALLS: ")) }
# if using all_pages, I will run all of the pagination available.
new_cursor <- TRUE
dd <- NULL
cursor <- NULL
while( new_cursor ){
if(show_progress){ print(paste0("- cursor: ", cursor, " / new_cursor: ", new_cursor )) }
r <- callAPI(database = database,
headers = headers,
filters = filters,
cursor = cursor)
new_cursor <- r$has_more
cursor <- r$next_cursor
# stack the data.frames together
tmp <- getItemsAndFlattenIntoDataFrame( r$results )
dd <- dplyr::bind_rows(dd, tmp)
if(show_progress){ print(paste0("- nrow of downloads: ", nrow(dd) )) }
}
}else{
# no pagination, just the top 100
if(show_progress){ print(paste0("++++ NO PAGINATION: ")) }
cursor <- NULL
r <- callAPI(database = database,
headers = headers,
filters = filters,
cursor = cursor)
dd <- getItemsAndFlattenIntoDataFrame( r$results )
}
return(dd)
}
df <- getNotionDatabase(secret = Notion_secret, database = Notion_database)
# Sample 1 quotes ---------------------------------------------------------
data_s1 <- df[sample(1:nrow(df), 1), ] # Sample rows of data with Base R
quotes <- data_s1$properties.Quote.title.plain_text
d <- as.data.frame(quotes)
Quote1 <- d$quotes[1]
# Quote2 <- d$quotes[2]
# Telegram_message <- glue({Quote1},{Quote2}, .sep = ". \n \n")
Telegram_message <- Quote1
# Telegram Message --------------------------------------------------------
## Create the bot object
bot <- TGBot$new(token = Telegram_token)
## Set Chat ID
bot$set_default_chat_id(Telegram_chatID)
#Send Message
bot$sendMessage(Telegram_message)