En artículos anteriores, analizamos el tema de la construcción de bots con suficiente detalle, desde enviar el primer mensaje hasta programar un diálogo lógico con el bot.
Este es el último artículo de esta serie, en el que veremos cómo administrar los derechos para usar métodos de bot individuales en diferentes niveles.
Todos los artículos de la serie "Escribir un bot de telegrama en lenguaje 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 . , , , . , .
Buena suerte en la construcción de bots. En los comentarios, puede escribir ejemplos de sus bots y cómo los usa en la práctica.