最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How should I edit the code for sending messages from Google Script to Discord? - Stack Overflow

programmeradmin1浏览0评论
function doPost(e) {
  // ตรวจสอบว่ามีข้อมูลส่งมาจาก Tradingview เข้ามาหรือไม่
  if (!e || !e.postData || !e.postData.contents) {
    return ContentService.createTextOutput("No POST data received.");
  }
  
  // Discord Webhook URL
  const discordWebhookUrl = "ME";
  
  // รับข้อความจาก post data และตัดช่องว่างหัวท้ายออก
  let inputMessage = e.postData.contents.trim();
  
  // ใช้ PropertiesService ในการเก็บสถานะ Trend (Bullish หรือ Bearish)
  const scriptProperties = PropertiesService.getScriptProperties();
  let currentTrend = scriptProperties.getProperty("currentTrend");
  
  // ตรวจสอบข้อความสำหรับเปลี่ยน Trend
  if (inputMessage === "Bullish") {
    currentTrend = "Bullish";
    scriptProperties.setProperty("currentTrend", currentTrend);
    // สามารถเลือกส่งข้อความแจ้งอัพเดท Trend ได้ (ถ้าต้องการ)
    // inputMessage = "Trend เปลี่ยนเป็น Bullish";
  } else if (inputMessage === "Bearish") {
    currentTrend = "Bearish";
    scriptProperties.setProperty("currentTrend", currentTrend);
    // inputMessage = "Trend เปลี่ยนเป็น Bearish";
  }
  
  // ตรวจสอบ Trade Signal เมื่อมี Trend อยู่แล้ว (ใช้ Trend เดิมจนกว่าจะเปลี่ยน)
  if ((inputMessage === "Buy" || inputMessage === "Strong Buy") && currentTrend === "Bullish") {
    inputMessage = "Long USDJPY TF1";
  } else if ((inputMessage === "Sell" || inputMessage === "Strong Sell") && currentTrend === "Bearish") {
    inputMessage = "Short USDJPY TF1";
  }
  
  // เตรียม payload สำหรับส่งไปยัง Discord
  let payload = {
    content: inputMessage
  };
  
  // กำหนด options สำหรับ UrlFetchApp.fetch
  let options = {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };
  
  // ส่งข้อความไปที่ Discord ผ่าน Webhook
  try {
    let response = UrlFetchApp.fetch(discordWebhookUrl, options);
    return ContentService.createTextOutput("Success: " + response.getContentText());
  } catch (error) {
    return ContentService.createTextOutput("Error: " + error);
  }
}

I need send massage only "Long USDJPY TF1" and "Short USDJPY TF1".

"I tried editing the code, but when the program runs, it can send messages other than 'Long USDJPY TF1' and 'Short USDJPY TF1' to Discord, such as 'Eat', 'Run' from TradingView alerts. Since I'm a beginner, I'm slowly analyzing the code in GPT."

function doPost(e) {
  // ตรวจสอบว่ามีข้อมูลส่งมาจาก Tradingview เข้ามาหรือไม่
  if (!e || !e.postData || !e.postData.contents) {
    return ContentService.createTextOutput("No POST data received.");
  }
  
  // Discord Webhook URL
  const discordWebhookUrl = "ME";
  
  // รับข้อความจาก post data และตัดช่องว่างหัวท้ายออก
  let inputMessage = e.postData.contents.trim();
  
  // ใช้ PropertiesService ในการเก็บสถานะ Trend (Bullish หรือ Bearish)
  const scriptProperties = PropertiesService.getScriptProperties();
  let currentTrend = scriptProperties.getProperty("currentTrend");
  
  // ตรวจสอบข้อความสำหรับเปลี่ยน Trend
  if (inputMessage === "Bullish") {
    currentTrend = "Bullish";
    scriptProperties.setProperty("currentTrend", currentTrend);
    // สามารถเลือกส่งข้อความแจ้งอัพเดท Trend ได้ (ถ้าต้องการ)
    // inputMessage = "Trend เปลี่ยนเป็น Bullish";
  } else if (inputMessage === "Bearish") {
    currentTrend = "Bearish";
    scriptProperties.setProperty("currentTrend", currentTrend);
    // inputMessage = "Trend เปลี่ยนเป็น Bearish";
  }
  
  // ตรวจสอบ Trade Signal เมื่อมี Trend อยู่แล้ว (ใช้ Trend เดิมจนกว่าจะเปลี่ยน)
  if ((inputMessage === "Buy" || inputMessage === "Strong Buy") && currentTrend === "Bullish") {
    inputMessage = "Long USDJPY TF1";
  } else if ((inputMessage === "Sell" || inputMessage === "Strong Sell") && currentTrend === "Bearish") {
    inputMessage = "Short USDJPY TF1";
  }
  
  // เตรียม payload สำหรับส่งไปยัง Discord
  let payload = {
    content: inputMessage
  };
  
  // กำหนด options สำหรับ UrlFetchApp.fetch
  let options = {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };
  
  // ส่งข้อความไปที่ Discord ผ่าน Webhook
  try {
    let response = UrlFetchApp.fetch(discordWebhookUrl, options);
    return ContentService.createTextOutput("Success: " + response.getContentText());
  } catch (error) {
    return ContentService.createTextOutput("Error: " + error);
  }
}

I need send massage only "Long USDJPY TF1" and "Short USDJPY TF1".

"I tried editing the code, but when the program runs, it can send messages other than 'Long USDJPY TF1' and 'Short USDJPY TF1' to Discord, such as 'Eat', 'Run' from TradingView alerts. Since I'm a beginner, I'm slowly analyzing the code in GPT."

Share Improve this question asked Feb 7 at 16:39 GfourGfour 1 New contributor Gfour is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • Have you analyzed the conditions that set inputMessage to "Long USDJPY TF1" and "Short USDJPY TF1"? You need to determine what's failing in the conditionals, preventing the variable from being overwritten. – mykaf Commented Feb 7 at 16:58
  • I ran your code, and the data is passed without any issues after execution. Could you tell me what data is being passed to the doPost(e) function? – Lime Husky Commented Feb 7 at 17:03
Add a comment  | 

1 Answer 1

Reset to default 0

The question implies that UrlFetchApp.fetch() should only be called when input is 'Buy' or 'Strong Buy' and the current trend is 'Bullish', or when input is 'Sell' or 'Strong Sell' and the current trend is 'Bearish'. To do that, add a conditional that exits the function before sending the request when there is no output, like this:

function doPost(e) {
  'use strict';
  if (!e?.postData?.contents) return ContentService.createTextOutput('No POST data received.');
  const input = e.postData.contents.trim();
  const scriptProperties = PropertiesService.getScriptProperties();
  let currentTrend = scriptProperties.getProperty('currentTrend');
  if (['Bullish', 'Bearish'].includes(input)) {
    currentTrend = input;
    scriptProperties.setProperty('currentTrend', input);
  }
  let output;
  if (['Buy', 'Strong Buy'].includes(input) && currentTrend === 'Bullish') output = 'Long USDJPY TF1';
  if (['Sell', 'Strong Sell'].includes(input) && currentTrend === 'Bearish') output = 'Short USDJPY TF1';
  if (!output) return;
  const options = {
    method: 'post',
    contentType: 'application/json',
    payload: JSON.stringify({ content: output }),
    muteHttpExceptions: true
  };
  try {
    const discordWebhookUrl = 'ME';
    const response = UrlFetchApp.fetch(discordWebhookUrl, options);
    return ContentService.createTextOutput('Success: ' + response.getContentText());
  } catch (error) {
    return ContentService.createTextOutput('Error: ' + error);
  }
}

See How to debug small programs and Clean Code JavaScript.

发布评论

评论列表(0)

  1. 暂无评论