loumadev / EdupageAPI

Simple node.js package to manage your EduPage account.
GNU General Public License v3.0
26 stars 2 forks source link

Listening for a new items (messages) on the timeline #4

Closed ghost closed 3 years ago

loumadev commented 3 years ago

Hello, what do you mean by fetching new messages from Edupage?

Do you mean it like:

Specify the request, please, so I can help you correctly.

loumadev commented 3 years ago

For this purpose, you can use the following snippet:

const {Edupage, TIMELINE_ITEM_TYPE} = require("edupage-api");

const edupage = new Edupage();
const MESSAGE_UPDATE_INTERVAL_MS = 10000;  //After this time the messages get refreshed (smaller value = smaller delay, but highier load)

(async () => {
    await edupage.login(USERNAME, PASSWORD);

    //Keep track of an old messages
    let oldTimeline = edupage.timeline;

    //Create an update interval
    setInterval(async () => {
        //Every `MESSAGE_UPDATE_INTERVAL_MS` milliseconds refresh all messages
        await edupage.refreshTimeline();

        const newMessages = edupage.timeline
            //Filter out old messages (required)
            .filter(e => !oldTimeline.find(t => t.id == e.id))
            //Filter out messages created by the current user (i.e. you are sending a message, but the message on the timeline apears anyway so callback fires) (optional)
            .filter(e => e.owner != edupage.user)
            //Filter out all timeline items except messages (those sent directly by the users) (optional)
            .filter(e => e.type == TIMELINE_ITEM_TYPE.MESSAGE);

        //Mark new messages as old so you can identify new ones on the next interval iteration
        oldTimeline = edupage.timeline;

        //If there are any new messages, run this statement (you can consider this as a callback function)
        if(newMessages.length) {
            //Your code here...
            console.log(newMessages);
        }
    }, MESSAGE_UPDATE_INTERVAL_MS);
})();

You could also wrap it into some MessageListener class for example to make it more flexible.

loumadev commented 3 years ago

Could you please resend the image of the error or rather send it as text. I can't see the image, my github is little strange now. (Even code highlighting is not working. Probably because I'm kinda busy now and can't have access my PC so I'm using only my mobile phone.)

loumadev commented 3 years ago

I have released a new version, so please update the current one by npm update edupage-api. It should help with crashing the program, but the bug itself isn't fixed. You will probably get some warnings, so you may want to post these warnings as a new issue, so I could try to fix them. It's really hard to debug such a messy, undocumented project with tons of variations, it's practically impossible since I don't have your data, which cause the issues.