Dans les articles précédents, nous avons analysé le sujet de la construction de robots de manière suffisamment détaillée, de l'envoi du premier message à la programmation d'un dialogue logique avec le bot.
C'est le dernier article de cette série, dans lequel nous allons comprendre comment gérer les droits d'utilisation des méthodes de bot individuelles à différents niveaux.
Tous les articles de la série "Ecrire un robot de télégramme en langage R"
, , .
.. . , . , , .
, .
, 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 . , , , . , .
Bonne chance dans la construction de robots. Dans les commentaires, vous pouvez écrire des exemples de vos robots et comment vous les utilisez dans la pratique.