When is the object preceded by al and when is the accusative used?
Should I use diras al vi or ŝatas vin?
I heard that sometimes both versions are allowed.
The linguistic term is case government (rekcio in Esperanto) which refers to how non-subject nouns and pronouns connect to a verb.
There are two major roles such a noun/pronoun can have: either it is the direct object of the action ("I like you") or it is the recipient or beneficiary of an action ("I talk to you").
When having the role of a direct object a noun/pronoun and its possible adjective attributes are in accusative – taking the ending -n – in Esperanto.
The accusative case is quite likely the most common case – besides the base, nominative case – in languages with a case system. (See a Youtube video for a quick introduction to cases.)
Other languages may have additional cases (e.g. partitive in Finnish), pre- or postpositions (lo in Spanish, を in Japanese) or just rely on the word order (English).
While some languages use a grammatical case (often dative) for marking a recipient or beneficiary of the verb's action (e.g. German: Ich gab dem Mann das Buch: I gave the book to the man.), Esperanto mostly uses the preposition al for this purpose.
Compare this to English which relies on the word order ("I sent you a letter") or uses the preposition "to" ("I sent a letter to you").
There is a handful of verbs with which a noun/pronoun can be interpreted to be an object or a recipient.
Note, that here in the latter case min is also possible, while mi helpas al vi is rarely used.
If one's native language has accusative for a direct object, accusative in Esperanto is often a no-brainer. Otherwise you just simply have to learn, which may sound harsh, but let me assure you that quite quickly you develop a sense whether we are talking about an object or a recipient.
Here helps a good dictionary, like PIV 2020, which tells whether a verb is transitive or not (e.g. in PIV marked (tr) for transitive and (ntr) for intransitive), i.e. whether it takes a direct object or not. Furthermore a good dictionary should provide information about case government and have some examples of use.