This really should be in the scripting forum.
But whatever.
However, I will not give you the whole script.
Rather a psuedocode for it.
Essentially, it is like this.
NPC Code:
if (created) {
Initialize Variables,etc;
}
if (Player says the right thing) {
Set the door to be unlocked;
Or if they say the thing to close
Set the door to locked;
}
if (playertouchsme) {
if (Check if Open) {
Open;
}
if not open
Display message;
}
There.
Something like that, but with real code instead.
I could just give you the script, but you don't learn like that, you'll end up a much better scripter if you experiment and find what works and what doesn't.
Hint: Don't use if (playersays(stuff)) ever!