You can do that sort of thing in Asterisk, and Asterisk will run on a Raspberry Pi.
My approach is to give unrecognised callers a burst of SIT (which causes most spammers to drop the call and remove you from their database), followed by a "press 1 to continue" Turing test (if they pass, it rings the phones, if they fail, straight to voicemail). Repeat offenders get sent straight to the Rickroll extension. Numbers belonging to barakta's mum and known accomplices ring the phones with a different cadence. Everything goes to voicemail when we're not at home or in bed (which the computer knows for other reasons).
The bad news is that Asterisk has a fairly horrendous learning curve (the extension language syntax is frankly bizarre), and that getting analogue telephony in and out of a Raspberry Pi is non-trivial: ATAs are invariably pants, and TDM cards require a PCI slot, which the Pi doesn't have. Both are horrendously expensive.
In short: Asterisk or similar is an excellent idea if your telephony is already standards-based VOIP. It's a good idea if you want to learn Asterisk. It's a hammer to crack a walnut if you want to avoid spam.