R言語でテレグラムボットを作成します(パート1):ボットを作成し、それを使用してテレグラムでメッセージを送信します

電報の視聴者は毎日指数関数的に増加しています。これは、メッセンジャーの利便性、チャネル、チャットの存在、そしてもちろんボットを作成する機能によって促進されます。



ボットは、顧客との通信の自動化から独自のタスクの管理まで、まったく異なる目的に使用できます。



実際、ボットを介して、テレグラムを使用して、データの送信または要求、サーバーでのタスクの実行、データベースへの情報の収集、電子メールの送信など、あらゆる操作を実行できます。



RでテレグラムボットAPIを操作し、必要に応じてボットを作成する方法に関する一連の記事を作成する予定です





この最初の記事では、テレグラムボットを作成し、それを使用してテレグラムで通知を送信する方法を理解します。



その結果、Windowsタスクスケジューラ内のすべてのタスクの最後の実行のステータスをチェックし、一部が失敗した場合に通知を送信するボットが作成されます。



, , , telegram.bot, , , .



" telegram R"



  1. , telegram
  2. ,




telegram youtube . R.



  1. R
  2. R Telegram
  3. Emoji
  4. Windows, ,




. BotFather, /start.



:



BotFather
I can help you create and manage Telegram bots. If you're new to the Bot API, please see the manual (https://core.telegram.org/bots).

You can control me by sending these commands:

/newbot - create a new bot
/mybots - edit your bots [beta]

Edit Bots
/setname - change a bot's name
/setdescription - change bot description
/setabouttext - change bot about info
/setuserpic - change bot profile photo
/setcommands - change the list of commands
/deletebot - delete a bot

Bot Settings
/token - generate authorization token
/revoke - revoke bot access token
/setinline - toggle inline mode (https://core.telegram.org/bots/inline)
/setinlinegeo - toggle inline location requests (https://core.telegram.org/bots/inline#location-based-results)
/setinlinefeedback - change inline feedback (https://core.telegram.org/bots/inline#collecting-feedback) settings
/setjoingroups - can your bot be added to groups?
/setprivacy - toggle privacy mode (https://core.telegram.org/bots#privacy-mode) in groups

Games
/mygames - edit your games (https://core.telegram.org/bots/games) [beta]
/newgame - create a new game (https://core.telegram.org/bots/games)
/listgames - get a list of your games
/editgame - edit a game
/deletegame - delete an existing game


/newbot.



BotFather .



BotFather, [25.07.20 09:39]
Alright, a new bot. How are we going to call it? Please choose a name for your bot.

Alexey Seleznev, [25.07.20 09:40]
My Test Bot

BotFather, [25.07.20 09:40]
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.

Alexey Seleznev, [25.07.20 09:40]
@my_test_bot


, bot.



, :



Done! Congratulations on your new bot. You will find it at t.me/my_test_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
123456789:abcdefghijklmnopqrstuvwxyz

For a description of the Bot API, see this page: https://core.telegram.org/bots/api


API , 123456789:abcdefghijklmnopqrstuvwxyz.



BotFather . .



R



, R, RStudio. , , .



Telegram Bot API R telegram.bot.



R install.packages(), install.packages("telegram.bot").



.



:



library(telegram.bot)


R Telegram



Telegram , @my_test_bot.



, " ". , id .



R .



library(telegram.bot)

#   
bot <- Bot(token = "123456789:abcdefghijklmnopqrstuvwxyz")

#    
print(bot$getMe())

#   , ..    
updates <- bot$getUpdates()

#   
# :        
chat_id <- updates[[1L]]$from_chat_id()


Bot(), .



, , . telegram.bot : R_TELEGRAM_BOT___. __ , R_TELEGRAM_BOT_My Test Bot.



, . ( path.expand("~")) .Renviron. file.edit(path.expand(file.path("~", ".Renviron"))).



.



R_TELEGRAM_BOT___=123456789:abcdefghijklmnopqrstuvwxyz


bot_token(), .. :



bot <- Bot(token = bot_token("My Test Bot"))


getUpdates() , .. . from_chat_id(), , . .



id getUpdates() . , , .



updates[[1L]]$message$from


$id
[1] 000000000

$is_bot
[1] FALSE

$first_name
[1] "Alexey"

$last_name
[1] "Seleznev"

$username
[1] "AlexeySeleznev"

$language_code
[1] "ru"


, , . sendMessage(), , , . Markdown HTML parse_mode.



#  
bot$sendMessage(chat_id,
                text = ", * * __",
                parse_mode = "Markdown"
)


Markdown :



  • *:

    • : * *
    • :
  • :

    • : __
    • :
  • , , — `:

    • : ` `
    • :


HTML :

HTML , , , <></>.



  • <> —
  • </> —


HTML



  • <b>

    • : <b> </b>
  • <i>

    • : <i></i>
    • :
  • <code> —

    • : <code> </code>
    • :


:



#  
bot$sendPhoto(chat_id,
  photo = "https://telegram.org/img/t_logo.png"
)

#   
bot$sendAudio(chat_id,
  audio = "http://www.largesound.com/ashborytour/sound/brobob.mp3"
)

#  
bot$sendDocument(chat_id,
  document = "https://github.com/ebeneditos/telegram.bot/raw/gh-pages/docs/telegram.bot.pdf"
)

#  
bot$sendSticker(chat_id,
  sticker = "https://www.gstatic.com/webp/gallery/1.webp"
)

#  
bot$sendVideo(chat_id,
  video = "http://techslides.com/demos/sample-videos/small.mp4"
)

#  gif 
bot$sendAnimation(chat_id,
  animation = "https://media.giphy.com/media/sIIhZliB2McAo/giphy.gif"
)

#  
bot$sendLocation(chat_id,
  latitude = 51.521727,
  longitude = -0.117255
)

#    
bot$sendChatAction(chat_id,
  action = "typing"
)


.. sendPhoto() , ggplot2.



Emoji



, Emoji.



.



スマイリーテーブル



Unicode. , U+ \U000. .. , U+1F601, R — \U0001F601.



:



bot$sendMessage(chat_id, 
                '   \U0001F601     U+1F601')


:



Windows, ,



Windows taskscheduleR, dplyr.



#  
install.packages(c('taskscheduleR', 'dplyr'))
#  
library(taskscheduleR)
library(dplyr)


taskscheduler_ls() . filter() dplyr , 0, , 267011, , .



#   
task <- task <- taskscheduler_ls() %>%
        filter(! `Last Result`  %in% c("0", "267011") & 
               `Scheduled Task State` == "Enabled" & 
               Status != "Running") %>%
        select(TaskName) %>%
        unique() %>%
        unlist() %>%
        paste0(., collapse = "\n")


task , , Telegram.



, :



  • filter() — ,
  • select()
  • unique()
  • unlist()
  • paste0() — , , .. \n.


— .



bot$sendMessage(chat_id,
                text = task,
                parse_mode = "Markdown"
)


, :



#  
library(telegram.bot)
library(taskscheduleR)
library(dplyr)

#  
bot <- Bot(token = "123456789:abcdefghijklmnopqrstuvwxyz")

#  
chat_id <- 123456789

#   
task <- taskscheduler_ls() %>%
        filter(! `Last Result`  %in% c("0", "267011")  &
               `Scheduled Task State` == "Enabled" & 
               Status != "Running") %>%
        select(TaskName) %>%
        unique() %>%
        unlist() %>%
        paste0(., collapse = "\n")

#      
if ( task != "" ) {

  bot$sendMessage(chat_id,
                  text = task,
                  parse_mode = "Markdown"
  )

}


.



, , , .



, id . configr.



ini
[telegram_bot]
;    ,     
chat_id=12345678
bot_token=123456789:abcdefghijklmnopqrstuvwxyz"


R
library(configr)

#  
config <- read.config('C:/__/config.cfg', rcmd.parse = TRUE)

bot_token <- config$telegram_bot$bot_token
chat_id     <- config$telegram_bot$chat_id




. , . - , .



, check_bot.R. , :



  1. Path R, Windows : C:\Program Files\R\R-4.0.2\bin.
  2. bat , R CMD BATCH C:\rscripts\check_bot\check_bot.R. C:\rscripts\check_bot\check_bot.R R .
  3. Windows , .




, , telegram.



Windows, , , .. R .



, , , , .




All Articles