R/TheSecretary.R

Defines functions Start InitConfig WakeUp ChangeMood ListenFrom WriteTo GetMessages TweetYouTube

# ==================
# GLOBAL DEFINITIONS
# ==================

# Configurations
telegram.cnf <<- NULL
twitter.cnf <<- NULL

# Real Time Bots
telegram.bot  <<- NULL
telegram.msgs <<- NULL


# ==================
# Generic Bot Status
# ==================

#' Start
#'
#' @param telegram 
#' @param twitter 
#'
#' @return
#' @export
#'
#' @examples
Start <- function(telegram = "", twitter = ""){
    options(httr_oauth_cache = TRUE)
    InitConfig(telegram, twitter)
    WakeUp()
}

#' InitConfig
#'
#' @param telegram 
#' @param twitter 
#'
#' @return
#' @export
#'
#' @examples
InitConfig <- function(telegram = "",
                       twitter  = ""){
    if (nchar(telegram) > 0) {
        telegram.cnf <<- yaml::yaml.load_file(telegram)
    }
    if (nchar(twitter) > 0 ){
        twitter.cnf  <<- yaml::yaml.load_file(twitter)
    }
}

#' WakeUp
#'
#' @param config 
#'
#' @return logical , TRUE if ok.
#' @export
#'
#' @examples if (WakeUp) {...}
WakeUp <- function(who = "all"){
    if (any(who %in% c("all","telegram"))) {
        ConnectTelegram()
        if (is.null(telegram.bot)) {
            warning("[warn] Something went wrong trying to wake up Telegram bot.")
            return(FALSE)
        }
    }
    if (any(who %in% c("all","twitter"))) {
        twitteR::setup_twitter_oauth(twitter.cnf$consumer_key, 
                                 twitter.cnf$consumer_secret,
                                 twitter.cnf$access_token,
                                 twitter.cnf$access_token_secret)
    }
    return(TRUE)
}

#' ChangeMood
#' 
#' I don't really know, but maybe sleep will disconnect and close all
#'
#' @param modus , character default "ready", nothing implemented.
#'
#' @return
#' @export
#'
#' @examples
ChangeMood <- function(modus = "sleep") {
    if (modus == "sleep") {
        telegram.bot <<- NULL
        return(TRUE)
    } 
    return(FALSE)
}

# ======================
# Read Generic Functions
# ======================

#' ListenFrom
#'
#' @param conn 
#' @param ... 
#'
#' @return
#' @export
#'
#' @examples
ListenFrom <- function(conn = "telegram", chat.id = 0) {
    msgs <- data.frame(message.id = as.numeric(),
                       name = as.character(),
                       text = as.character(),
                       stringsAsFactors = FALSE)
    if (conn == "telegram" & exists("chat.id")) {
        telegram.msgs <<- TellMe(chat.id)
        msgs <- rbind(msgs, telegram.msgs)
    }
    if (conn == "twitter"){
#         twitter.msgs <<- data.frame(stringsAsFactors = FALSE)
#         msgs <- rbind(msgs, twitter.msgs)
    }
    return(msgs)
}

# =======================
# Write Generic Functions
# =======================

#' WriteTo
#'
#' @param conn 
#' @param ... 
#'
#' @return
#' @export
#'
#' @examples
WriteTo <- function(conn = "telegram", text = "", chat = 0) {
    if (conn == "telegram") {
        secretary::CheatIt(text, chat)
    } else {
        if (conn == "twitter") {
            twitteR::tweet(text)
        }
    }
}

# ================
# Generic Messages
# ================

#' GetMessages
#'
#' @param type 
#'
#' @return data.frame
#' @export
#'
#' @examples
GetMessages <- function(type = "telegram") {
    msgs <- data.frame(stringsAsFactors = FALSE)
    return(msgs)
}

# =====================
# Other functionalities
# =====================

#' TweetYouTube Personal functionality. It reads from a group chat and tweet
#' all youtube links.
#'
#' @param chat.id 
#'
#' @export
#'
#' @examples
TweetYouTube <- function(chat.id = 0){
    # Get telegram messages from chat defined
    msgs <- secretary::ListenFrom("telegram", chat.id)
    # Analize messages from listen chat searching for youtube links
    # when it found links, it write the same message to post chat
    i <- 1
    while (i <= nrow(msgs)) {
        # Get text for analyze
        msg.id <- msgs[i,c("message.id")]
        text <- msgs[i,c("text")]
        if (!is.na(text)) {
            # Analize with 2 level pattern search
            url <- stringr::str_extract(text, telegram.cnf$patternLvl1)
            hit <- stringr::str_extract(url, telegram.cnf$patternLvl2)
            if (!is.na(hit)){
                WriteTo("telegram", text, telegram.cnf$chat.home)
                twit <- paste(hit, "by", msgs[i,c("name")], "#NowPlaying #AFC")
                WriteTo("twitter", twit)
                saveRDS(object = msg.id, file = "inst/extdata/telegram_id.rds")
            }
        }
        i <- i + 1
    }
}
humbertcostas/LaSecre documentation built on May 17, 2019, 9:13 p.m.