为什么此Webhook在RStudio中有效,但在命令行中无效?

时间:2019-05-12 18:50:16

标签: r cmd webhooks slack

我构建了一个简单的脚本,该脚本定期将.txt的内容上传到Slack频道。

当我在RStudio中运行它时,它工作正常,但是当我通过批处理文件从命令行运行它时,它不起作用。该代码运行,打印预期的输出并且不会崩溃,但是所选的Slack通道中没有任何内容。发生了什么事?

logger.r

    #required packages:    
    require(httr)
    require(tidyverse)
    library(jsonlite)

    options(stringsAsFactors = F)

    #System directories:
    winLogRoot = "C:/Users/Dave/source/repos/window logger/Release"
    current = paste0(winLogRoot,"/example.txt")

    #httr stuff:
    url = "https://hooks.slack.com/services/[webhook url here]"



    waitTime = 60 # how often to check the text file
    while(1)
    {
      #Get first row of table
      winName = read.table(file = current, sep = "\n")
      sessHist = tibble(ts = as.character(Sys.time()),winName = paste0(winName$V1,"\n"))

      for(waits in 1:(300/waitTime)) # Send to slack every five minutes
      {
        print(waits) #debug string
        Sys.sleep(waitTime)
        winName = read.table(file = current, sep = "\n")
        ## Add another row to the tibble
        sessHist = rbind(sessHist,tibble(ts = as.character(Sys.time()), winName = paste0(winName$V1,"\n")))
      }
      #Turn the tibble into a string and post it to Slack
      print("attemping upload") #debug string
      body = toString(sessHist%>%as.matrix %>% t) 
      body = paste0('{"text":"',body,'"}')
      print(body) #debug string

#This is the bit that only seems to work when I run in RStudio
      httr::POST(url = url,body=body,encode = 'json')
    }

logger.bat

"C:\Program Files\R\R-3.5.3\bin\i386\Rscript.exe" logger.R

命令行输出

C:\Users\Dave\Documents\logger\win logger Rpart>"C:\Program Files\R\R-3.5.3\bin\i386\Rscript.exe" logger.R
Loading required package: httr
Loading required package: tidyverse
-- Attaching packages --------------------------------------- tidyverse 1.2.1 --
v ggplot2 3.1.0       v purrr   0.3.1
v tibble  2.0.1       v dplyr   0.8.0.1
v tidyr   0.8.3       v stringr 1.4.0
v readr   1.3.1       v forcats 0.4.0
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 15:26:46, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:32:55, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:39:05, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:45:16, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:51:26, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 15:57:36, C:\\WINDOWS\\system32\\cmd.exe\n\"}"
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 15:57:37, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:03:47, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:09:57, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:16:07, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:22:17, C:\\WINDOWS\\system32\\cmd.exe\n, 2019-05-12 16:28:27, C:\\WINDOWS\\system32\\cmd.exe\n\"}"
[1] 1
[1] 2

因此,显然它正在运行脚本,但未成功使用Webhook。

奖金内容:存在危机(文本文件包含前台窗口的名称)

[1] 5
[1] "attemping upload"
[1] "{\"text\":\"2019-05-12 19:45:38, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 19:51:41, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 19:57:44, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n, 2019-05-12 20:03:48, Why does this webhook work in RStudio, but not command line? - Stack Overflow - Google Chrome\n\"}"
[1] 1
[1] 2

1 个答案:

答案 0 :(得分:0)

所以我重新启动了计算机,现在可以正常工作了。我猜这就是答案吗?但是,感谢您提供有关位之类的建议。很高兴我不必解决这个问题。 :)