Writing a telegram bot in R (part 5): Managing the rights of bot users

In previous articles, we analyzed the topic of bot building in sufficient detail, from sending the first message to programming a logical dialogue with the bot.



This is the last article in this series, in which we will figure out how to manage the rights to use individual bot methods at different levels.





All articles from the series "Writing a telegram bot in the R language"



  1. We create a bot and send messages to telegram using it
  2. ,




telegram youtube . R.





  1. 2.1.

    2.2.


  2. 3.1.

    3.2.




, , .



.. . , . , , .



, .



, 2 :



  • say_hello
  • what_time — ,


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)

}

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

  #   
  cur_time <- as.character(Sys.time())

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

}

# 
h_hello <- CommandHandler('say_hello', say_hello)
h_time  <- CommandHandler('what_time', what_time)

#    
updater <- updater + h_hello + h_time

#   
updater$start_polling()


, ' ' , BotFather ( ).

, , .





, . , , - , .



, .





BaseFilter() MessageFilters. .



, BaseFilter()message. , , . :



$message_id
[1] 1174

$from
$from$id
[1] 194336771

$from$is_bot
[1] FALSE

$from$first_name
[1] "Alexey"

$from$last_name
[1] "Seleznev"

$from$username
[1] "AlexeySeleznev"

$from$language_code
[1] "ru"

$chat
$chat$id
[1] 194336771

$chat$first_name
[1] "Alexey"

$chat$last_name
[1] "Seleznev"

$chat$username
[1] "AlexeySeleznev"

$chat$type
[1] "private"

$date
[1] 1601295189

$text
[1] "  "

$chat_id
[1] 194336771

$from_user
[1] 194336771


, , . , :



##  ,   
MessageFilters$admins <- BaseFilter(
  function(message) {

    #     
    message$from$username %in% c('AlexeySeleznev', 'user1', 'user2')

 }
)


c('AlexeySeleznev', 'user1', 'user2') — , , . .



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

    #     
    message$text == '/say_hallo'

  }
)

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

    #     
    message$text == '/what_time'

  }
)

# 
h_hello <- MessageHandler(say_hello, MessageFilters$admins & MessageFilters$say_hello)
h_time  <- MessageHandler(what_time, MessageFilters$admins & MessageFilters$what_time)


AlexeySeleznev, user1, user2. .



:



,
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)

}

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

  #   
  cur_time <- as.character(Sys.time())

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

}

# 
##  ,   
MessageFilters$admins <- BaseFilter(
  function(message) {

    #     
    message$from$username %in% c('AlexeySeleznev', 'user1', 'user2')

 }
)

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

    #     
    message$text == '/say_hallo'

  }
)

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

    #     
    message$text == '/what_time'

  }
)

# 
h_hello <- MessageHandler(say_hello, MessageFilters$admins & MessageFilters$say_hello)
h_time  <- MessageHandler(what_time, MessageFilters$admins & MessageFilters$what_time)

#    
updater <- updater + h_hello + h_time

#   
updater$start_polling()




, . :



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

    #     
    message$chat_id %in% c(194336771, 0, 1)

  }
)

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

    #     
    message$text == '/say_hallo'

  }
)

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

    #     
    message$text == '/what_time'

  }
)

# 
h_hello <- MessageHandler(say_hello, MessageFilters$admins & MessageFilters$chats & MessageFilters$say_hello)
h_time  <- MessageHandler(what_time, MessageFilters$admins & MessageFilters$chats & MessageFilters$what_time)




, .





, .



#     
bot_check_usernames <- 
  function(admins, username) {

   username %in% admins 

}


admins , , username , .



, IF , . .



, , , what_time.



,
library(telegram.bot)

#    Updater
updater <- Updater('  ')

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

  #      
  user_name <- update$message$from$username

  #        
  if ( bot_check_usernames(c('AlexeySeleznev', 'user1', 'user2'), user_name) ) {

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

  } else {

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

  }

}

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

  #        
  if ( bot_check_usernames(c('user1', 'user2'), update$message$from$username) ) {

    #   
    cur_time <- as.character(Sys.time())

    #          
    bot$sendMessage(update$message$chat_id, 
                    text = paste0(" , ", cur_time),
                                  parse_mode = "Markdown",
                                  reply_to_message_id = update$message$message_id)
  } else {

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

  }

}

# 
h_hello <- CommandHandler('say_hello', say_hello)
h_time  <- CommandHandler('what_time', what_time)

#    
updater <- updater + h_hello + h_time

#   
updater$start_polling()


:





, , .





, , , .



, .



bot_check_chat_id <- 
  function(allowed_chats, current_chat) {

     current_chat %in% allowed_chats 

}


:



library(telegram.bot)

#    Updater
updater <- Updater('  ')

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

  #      
  user_name <- update$message$from$username

  #        
  if ( bot_check_usernames(c('AlexeySeleznev', 'user1', 'user2'), user_name) 
       &
       bot_check_chat_id(c(194336771, 1, 2), update$message$chat_id)) {

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

  } else {

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

  }

}

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

  #        
  if ( bot_check_usernames(c('AlexeySeleznev', 'user1', 'user2'), update$message$from$username)
       &
       bot_check_chat_id(c(194336771, 1, 2), update$message$chat_id)) {

    #   
    cur_time <- as.character(Sys.time())

    #          
    bot$sendMessage(update$message$chat_id, 
                    text = paste0(" , ", cur_time),
                                  parse_mode = "Markdown",
                                  reply_to_message_id = update$message$message_id)
  } else {

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

  }

}

# 
h_hello <- CommandHandler('say_hello', say_hello)
h_time  <- CommandHandler('what_time', what_time)

#    
updater <- updater + h_hello + h_time

#   
updater$start_polling()




telegram . , , , . , .



Good luck in bot building. In the comments, you can write examples of your bots, and how you use them in practice.




All Articles