用R语言编写电报机器人(第2部分):向机器人添加命令支持和消息过滤器

一篇文章中,我们了解了如何创建机器人,初始化该类的实例Bot以及熟悉使用该机器人发送消息的方法。



在本文中,我将继续该主题,因此,我建议仅在阅读第一部分之后才开始阅读本文



这次,我们将弄清楚如何使我们的机器人恢复活力并为其添加命令支持,以及了解该类Updater



在本文中,我们将编写几个简单的机器人,机器人将根据给定的日期和国家/地区代码,根据生产日历确定给定国家/地区的一天是周末还是工作日。但是,像以前一样,本文的目的是使您熟悉telegram.bot用于解决自己的问题的包接口





该系列的所有文章“用R语言编写电报机器人”



  1. 我们创建一个机器人并使用它发送消息到电报
  2. 向机器人添加命令支持和消息过滤器
  3. 如何为您的机器人增加键盘支持
  4. 与机器人建立一致的逻辑对话
  5. Bot用户权限管理


内容



telegram youtube . R.



  1. Updater
  2. Handlers —
  3. ,


Updater



Updater — , , Dispetcher. Updater , ( getUpdates()), Dispetcher.



Dispetcher , .. Handler.



Handlers —



Dispetcher . telegram.bot :



  • MessageHandler —
  • CommandHandler —
  • CallbackQueryHandler — Inline
  • ErrorHandler —


,



, , , / .



, .. /hi.



1:
library(telegram.bot)

#    Updater
updater <- Updater('  ')

#    
say_hello <- function(bot, update) {

  #      
  user_name <- update$message$from$first_name

  #   
  bot$sendMessage(update$message$chat_id, 
                  text = paste0(" , ", user_name, "!"), 
                  parse_mode = "Markdown")

}

#   
hi_hendler <- CommandHandler('hi', say_hello)

#    
updater <- updater + hi_hendler

#  
updater$start_polling()


, ' ' , BotFather ( ).

start_polling() Updater, , .



, /hi.





/hi, .



.





  1. Updater;
  2. , .. . say_hello(). , — bot update, — args. bot, , , , . update , , , getUpdates(). args , ;
  3. , .. - . , - . /hi, hi_hendler <- CommandHandler('hi', say_hello). CommandHandler() , hi, . , say_hello, ;
  4. Updater. , , +, .. updater <- updater + hi_hendler. add_handler(), Dispatcher, : updater$dispatcher$add_handler();
  5. start_polling().




, , , - , . — MessageHandler.



MessageHandler . . /hi, , : , , , , .



- , .. . .



2:
library(telegram.bot)

#    Updater
updater <- Updater('  ')

#    
##  
say_hello <- function(bot, update) {

  #      
  user_name <- update$message$from$first_name

  #   
  bot$sendMessage(update$message$chat_id, 
                  text = paste0(" , ", user_name, "!"),
                  parse_mode = "Markdown",
                  reply_to_message_id = update$message$message_id)

}

#  
MessageFilters$hi <- BaseFilter(function(message) {

  # ,      : , , , , 
  grepl(x           = message$text, 
        pattern     = '||||',
        ignore.case = TRUE)
  }
)

#   
hi_hendler <- CommandHandler('hi', say_hello) #   hi
hi_txt_hnd <- MessageHandler(say_hello, filters = MessageFilters$hi)

#    
updater <- updater + 
             hi_hendler +
             hi_txt_hnd

#  
updater$start_polling()


, ' ' , BotFather ( ).

, :



, , . reply_to_message_id, sendMessage(), id . id : update$message$message_id.



, — BaseFilter():



#  
MessageFilters$hi <- BaseFilter( 

  #   
  function(message) {

    # ,       
    grepl(x           = message$text, 
          pattern     = '||||',
          ignore.case = TRUE)
  }

)


, MessageFilters, . MessageFilters hi, .



BaseFilter() . , — , TRUE FALSE. , , grepl() , |||| TRUE.



hi_txt_hnd <- MessageHandler(say_hello, filters = MessageFilters$hi). MessageHandler() — , , — . MessageFilters$hi.



, , hi_txt_hnd.



updater <- updater + 
             hi_hendler +
             hi_txt_hnd


, telegram.bot MessageFilters , :



  • all —
  • text —
  • command — , .. /
  • reply — ,
  • audio —
  • document —
  • photo —
  • sticker —
  • video —
  • voice —
  • contact —
  • location —
  • venue —
  • game —


|, & . , , :



handler <- MessageHandler(callback, 
  MessageFilters$video | MessageFilters$photo | MessageFilters$document
)




, , . , .



, .



API isdayoff.ru.



3: ,
library(telegram.bot)

#    Updater
updater <- Updater('  ')

#    
##  
check_date <- function(bot, update, args) {

  #  
  day     <- args[1]  # 
  country <- args[2]  # 

  #   
  if ( !grepl('\\d{4}-\\d{2}-\\d{2}', day) ) {

    # Send Custom Keyboard
    bot$sendMessage(update$message$chat_id, 
                    text = paste0(day, " -  ,     --"),
                    parse_mode = "Markdown")

  } else {
    day <- as.Date(day)
    #    POSIXtl
    y <- format(day, "%Y")
    m <- format(day, "%m")
    d <- format(day, "%d")

  }

  #   
  ##    
  ##     ru
  if ( ! country %in% c('ru', 'ua', 'by', 'kz', 'us') ) {

    # Send Custom Keyboard
    bot$sendMessage(update$message$chat_id, 
                    text = paste0(country, " -   ,  : ru, by, kz, ua, us.    ."),
                    parse_mode = "Markdown")

    country <- 'ru'

  }

  #    API
  #  HTTP 
  url <- paste0("https://isdayoff.ru/api/getdata?",
                "year=",  y, "&",
                "month=", m, "&",
                "day=",   d, "&",
                "cc=",    country, "&",
                "pre=1&",
                "covid=1")

  #  
  res <- readLines(url)

  #  
  out <- switch(res, 
                "0"   = " ",
                "1"   = " ",
                "2"   = "  ",
                "4"   = "covid-19",
                "100" = "  ",
                "101" = "  ",
                "199" = " ")

  #  
  bot$sendMessage(update$message$chat_id, 
                  text = paste0(day, " - ", out),
                  parse_mode = "Markdown")

}

#   
date_hendler <- CommandHandler('check_date', check_date, pass_args = TRUE)

#    
updater <- updater + date_hendler

#  
updater$start_polling()


, ' ' , BotFather ( ).

, check_date, .



, , , . , , .



, pass_args = TRUE CommandHandler(), , bot, updateargs. , . , .



, .







— .



:



  1. R. RStudio File, Save As....
  2. bin, R Path, .
  3. , 1 : R CMD BATCH C:\Users\Alsey\Documents\my_bot.R. C:\Users\Alsey\Documents\my_bot.R . , , .. . , txt bat.
  4. Windows, , %windir%\system32\taskschd.msc /s. .
  5. " ...".
  6. "" , " ".
  7. "", "". " " "", bat , .
  8. , .
  9. , "".


, , , .





, , . .



, , .



telegram youtube .




All Articles