Discord API messaging when another function returns a value

I am using python discord API https://discordpy.readthedocs.io/en/latest/api.html and I would like to have a forever loop(like a while loop) That checks with function whether on the site is a new content(I have already wrote a scrapper). In other words: if scrapper sees a new post it will return a value(link). I want to connect it with discord and when there is an output of that function, it will be sent to the text channel. I have completely no clue how to do that. The scrapper function is asynchronous. All that comes to my mind is to make a second thread and log in and then message manually through selenium

1 answer

  • answered 2020-07-10 14:45 FluidLight

    Use webhooks.

    Create a webhook then whenever your scraper gets new data, submit a POST request to the webhook url you created with the content parameter set to your data.

    Example:

    import requests
    
    WEBHOOK_URL = "https://discordapp.com/api/webhooks/123456789/qWerRYtyuqwfq" # Example webhook url
    
    def sendToDiscord(webhookUrl, data):
        return requests.post(webhookUrl, json={'content': data})
        
    data = myScraper.get_data() # Whenever there is data from your scraper
    sendToDiscord(WEBHOOK_URL, data) # Send it to Discord
    

    Note: You can format your message, add images etc... by using the appropriate params