首页 > 解决方案 > 为什么这个 webhook 在 RStudio 中有效,但在命令行中无效?

问题描述

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

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

记录器.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

标签: rcmdwebhooksslack

解决方案


所以我重新启动了我的电脑,现在它可以工作了。我想这就是答案?但是感谢您提供有关比特之类的建议。很高兴我不必解决这个问题。:)


推荐阅读