Marceeelll

DiscordWebhooksLibrary

Marceeelll Updated   
πŸš€ Introduction
Welcome to the TradingView PineScript Library for Discord Webhook Integration! This library is designed for traders and developers who use TradingView for technical analysis and want to integrate their trading strategies with Discord notifications.

Key Features:
* Embed Creation: Easily create rich and informative embeds for your Discord messages, allowing you to send detailed trading alerts and summaries.
* Flexible Webhook Formatting: Customize your Discord messages with options for usernames, avatars, and text content, providing a personalized touch to your notifications.
* Simple Integration: Designed with simplicity in mind, this library can be integrated into your existing Pine Script trading strategies without extensive coding knowledge.
* Real-time Alerts: Utilize TradingView's alert system to send real-time trade signals and market updates to your Discord server.

Compatibility:
This library is compatible with TradingView's Pine Script version 5.

πŸƒ Code Snippets and Usage Examples
The following examples demonstrate how to use the Discord Webhook Integration Library in your TradingView Pine Scripts. These snippets cover various scenarios, showcasing the flexibility and utility of the library.

Example 1: Simple Alert with Markdown in Embed Description
embedDesc = "This is a **bold** and _italic_ alert message with a [link](replace_with_your_link)"
embedJson = createEmbedJSON("Simple Alert", embedDesc, 12345)
content = discordWebhookJSON("Alert from Captain Hook", "Captain Hook", na, embedJson)

Example 2: Multiple Embeds with Different Markdown Styles
embedDesc1 = "First alert with **bold** text"
embedDesc2 = "Second alert with _italic_ text"
embedDesc3 = "Third alert with ~~strikethrough~~"

embedJson1 = createEmbedJSON("Alert 1", embedDesc1, 654321)
embedJson2 = createEmbedJSON("Alert 2", embedDesc2, 123456)
embedJson3 = createEmbedJSON("Alert 3", embedDesc3, 111111)

embeds = embedJson1 + "," + embedJson2 + "," + embedJson3
content = discordWebhookJSON("Multiple Alerts", "Captain Hook", na, embeds)

Example 3: Complex Alert with Full Markdown Usage in Embed
embedDesc = "Alert: **Price Breakout!**\\n\\n" +
 "*Symbol*: " + syminfo.ticker + "\\n" +
 "*Price*: $" + str.tostring(close) + "\\n\\n" +
 "[Click here for details](replace_with_your_link)"

embedJson = createEmbedJSON("Complex Alert", embedDesc, 16711680) // Red color
content = discordWebhookJSON("Complex Alert", "Captain Hook", na, embedJson)

Example 4: Advanced Technical Analysis Alert
rsiValue = ta.rsi(close, 14)
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)

taMessage = "RSI: " + str.tostring(rsiValue) + "\\nMACD: " + str.tostring(macdLine)
embedJson = createEmbedJSON("Technical Analysis Update", taMessage, 255) // Blue color
content = discordWebhookJSON("TA Alert", "Captain Hook", na, embedJson)

Example 5: Market Summary with Multiple Fields
counterTrend = "Your counter trend criterias"
counterTrendEmbed = createEmbedJSON(title = "Counter Trend", description = counterTrend, color = 15258703)

redFlags = "Your red flag criterias"
redFlagsEmbed = createEmbedJSON(title = "Red Flags", description = redFlags, color = 15229263)

embeds = counterTrendEmbed + "," + redFlagsEmbed

content = discordWebhookJSON(contentText = "Example of how a market analysis could look like", username = "Captain Hook", embeds = embeds)

🚨 Error Handling
Use Escape Characters Correctly: In message strings, remember to use \\n for new lines instead of \n. This ensures that the newline character is correctly interpreted in the JSON format.

It can be helpful to plot the json on the last candle
if barstate.islast
     label.new(bar_index, high, text=debugMessage, color=color.red, textcolor=color.white, yloc=yloc.abovebar)

πŸ”₯ FAQs
Q1: Can I send alerts for multiple conditions?
A: Yes, you can configure multiple conditions in your script. Use separate if statements for each condition and call the discordWebhookJSON function with the relevant message for each alert.

Q2: Why is my alert not triggering?
A: Ensure your alert conditions are correct and that you've properly set up the webhook in both your script and TradingView's alert configuration. Also, check for any syntax errors in your script.

Q3: How many alerts can I send to Discord?
A: While TradingView doesn't limit the number of alerts, Discord has rate limits for webhooks. Be mindful of these limits to avoid your webhook being temporarily blocked.

Q4: Can I customize the appearance of my Discord messages?
A: Yes, the createEmbedJSON function allows you to customize your messages with titles, descriptions, colors, and more. Experiment with different parameters to achieve the desired appearance.

Q5: Is it possible to include real-time data in alerts?
A: Yes, your script can include real-time price data, indicator values, or any other real-time data available in Pine Script.

Q6: How can I contribute to the library or suggest improvements?
A: You can provide feedback, suggest improvements, or contribute to the library's development through the community channels or contact points provided in the "Support and Community" section.

formatTimeframe()

discordWebhookJSON(contentText, username, avatar_url, embeds)
  Constructs a JSON string for a Discord webhook message. This string includes optional fields for content, username, avatar URL, and embeds.
  Parameters:
    contentText (string): (string, optional): The main text content of the webhook message. Default is 'na'.
    username (string): (string, optional): Overrides the default username of the webhook. Default is 'na'.
    avatar_url (string): (string, optional): Overrides the default avatar URL of the webhook. Default is 'na'.
    embeds (string): (string, optional): A string containing one or more embed JSON objects. This should be formatted correctly as a JSON array. Default is 'na'.

createEmbedJSON(title, description, color, authorName, authorUrl, authorIconUrl, fields)
  Creates a JSON string for a single embed object for a Discord webhook.
  Parameters:
    title (string): (string, optional): The title of the embed. Default is 'na' (not applicable).
    description (string): (string, optional): The description text of the embed. Supports basic formatting. Default is 'na'.
    color (int): (int, optional): The color code of the embed, typically in decimal format. Default is 'na'.
    authorName (string): (string, optional): The name of the author to display in the embed. Default is 'na'.
    authorUrl (string): (string, optional): The URL linked to the author's name. Default is 'na'.
    authorIconUrl (string): (string, optional): The URL of the icon to display next to the author's name. Default is 'na'.
    fields (string): (string, optional): A string containing one or more field JSON objects. This should be formatted correctly as a JSON array. Default is 'na'. Note: Use the 'createEmbedFieldJSON' function to generate these JSON field strings before adding them to the array.

createEmbedFieldJSON(name, value, inline)
  Creates a JSON string representing a single field object within an embed for a Discord webhook message.
  Parameters:
    name (string): (string): The name of the field, acting as a title for the field content.
    value (string): (string): The value of the field, containing the actual text or information you want to display within the field.
    inline (bool): (bool, optional): A boolean flag indicating whether the field should be displayed inline with other fields. If set to true, the field will be displayed on the same line as the next field

❀️ Please, support the work with like & comment! ❀️
Release Notes:
v2

New Features:
* Embed Footer Support: You can now enhance your embeds with custom footers. This addition allows for more informative and branded messages (footerText and footerIconUrl).

Updated:
createEmbedJSON(title, description, color, authorName, authorUrl, authorIconUrl, fields, footerText, footerIconUrl)
  Creates a JSON string for a single embed object for a Discord webhook.
  Parameters:
    title (string): (string, optional): The title of the embed. Default is 'na' (not applicable).
    description (string): (string, optional): The description text of the embed. Supports basic formatting. Default is 'na'.
    color (int): (int, optional): The color code of the embed, typically in decimal format. Default is 'na'.
    authorName (string): (string, optional): The name of the author to display in the embed. Default is 'na'.
    authorUrl (string): (string, optional): The URL linked to the author's name. Default is 'na'.
    authorIconUrl (string): (string, optional): The URL of the icon to display next to the author's name. Default is 'na'.
    fields (string): (string, optional): A string containing one or more field JSON objects. This should be formatted correctly as a JSON array. Default is 'na'. Note: Use the 'createEmbedFieldJSON' function to generate these JSON field strings before adding them to the array.
    footerText (string): (string, optional): The footer text to display in the embed.
    footerIconUrl (string): (string, optional): The URL of the icon to display next to the footer text.
Pine library

In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in a publication is governed by House Rules.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this library?

Copy the following line and paste it in your script.