# Type: Input # Category: Help # Description: Provides help information to user # Author: Andriy Lesyuk new Orangutan::Context( response => sub { my ($context, $user, $message) = @_; my @result = ( ); my @regexps = ( '^(?:Please )?help(?: me)?(?: please)?!*\.*$', '^(?:How )?can you help(?: me)?\?*!*\.*$', '^I need (?:your )?help!*\.*$', '^Show help!*\.*$', '^(?:Show )?F(?:requently )?A(?:sked )?Q(?:uestions)?!*\.*$' ); for (my $i = 0; $i < scalar @regexps; $i++) { if ($message =~ /$regexps[$i]/i) { push(@result, ($i > 3) ? 2 : 1); return @result; } } for (my $i = 0; $i < scalar @{$context->{'sections'}}; $i++) { if (ref $context->{'sections'}[$i]->{'question'} eq 'ARRAY') { foreach my $regexp (@{$context->{'sections'}[$i]->{'question'}}) { if ($message =~ /$regexp/i) { push(@result, $i + 3); return @result; } } } else { if ($message =~ /$context->{'sections'}[$i]->{'question'}/i) { push(@result, $i + 3); return @result; } } } return @result; }, handler => sub { my ($context, $user, $item, $number) = @_; if ($item == -2) { if (($number > 0) && ($number <= scalar @{$context->{'sections'}})) { $user->SendMessage($context->{'sections'}[$number-1]->{'answer'}); my $ncontext = $user->GetResponse('Number'); if ($ncontext) { $ncontext->InsertForeignHandler($context, 600); } } else { $user->SendMessage([ 'Question with such number does not exist...', 'No such question!', 'Nothing to show.' ]); } } elsif ($item == 1) { my $message = "A very short tutorial on how to speak to me:\n"; $message .= " o To start a task just type what are you doing -- Fixing bugs\n"; $message .= " o To specify start time when creating a task -- Writing documentation since 10:00\n"; $message .= " o To specify issue and/or activity -- Working on #16 (QA activities)\n"; $message .= " o To rename a task type new name and answer \"yes\" on the appropriate question\n"; $message .= " o To change start time -- Change start time to 14:00\n"; $message .= " o To stop a task -- Done\n"; $message .= " o To specify end time when stopping -- Stop at 19:00\n"; $message .= " o To remove a task -- Remove task\n"; $message .= " o To cancel an accidentally created task (right after creation) -- Cancel\n"; $message .= " o To add a past task -- 10:00 - 11:00: Developing core functions\n"; $message .= " o To see your current task -- Show task\n"; $message .= " o To list today tasks -- Show tasks\n"; $message .= " o To see time spent in last month -- How much time did I spend last month?\n"; $message .= "For more details see FAQ (type \"FAQ\")..."; $user->SendMessage($message); } elsif ($item == 2) { my $message = Orangutan::Context::Random( 'I can answer the following questions', 'Here are the questions I can answer', 'Ask any of these questions' ).":\n"; for (my $i = 0; $i < scalar @{$context->{'sections'}}; $i++) { $message .= sprintf(" %d. %s", $i + 1, @{$context->{'sections'}}[$i]->{'title'}); if ($user->IsCreator && defined(@{$context->{'sections'}}[$i]->{'weight'})) { $message .= sprintf(" [%d]", @{$context->{'sections'}}[$i]->{'weight'}); } $message .= "\n"; } $user->SendMessage($message); my $ncontext = $user->GetResponse('Number'); if ($ncontext) { $ncontext->InsertForeignHandler($context, 600); } } else { $user->SendMessage($context->{'sections'}[$item-3]->{'answer'}); } }, init => sub { my ($context) = @_; my @contexts = ( ); foreach my $context ($main::config->Contexts) { my $help = $context->GetHelp; if (defined($help)) { if (ref $help eq 'ARRAY') { foreach my $item (@{$help}) { my $matched = 0; if (ref $item->{'question'} eq 'ARRAY') { foreach my $regexp (@{$item->{'question'}}) { if ($item->{'title'} =~ /$regexp/i) { $matched = 1; last; } } } else { if ($item->{'title'} =~ /$item->{'question'}/i) { $matched = 1; } } $item->{'context'} = $context->GetID; push(@contexts, $item); if ($matched == 0) { $main::logger->Log("question does not match title: %s", $item->{'context'}); } } } else { my $matched = 0; if (ref $help->{'question'} eq 'ARRAY') { foreach my $regexp (@{$help->{'question'}}) { if ($help->{'title'} =~ /$regexp/i) { $matched = 1; last; } } } else { if ($help->{'title'} =~ /$help->{'question'}/i) { $matched = 1; } } $help->{'context'} = $context->GetID; push(@contexts, $help); if ($matched == 0) { $main::logger->Log("question does not match title: %s", $help->{'context'}); } } } } my @sections = sort { ((defined($a->{'weight'})) ? $a->{'weight'} : 0) <=> ((defined($b->{'weight'})) ? $b->{'weight'} : 0) } @contexts; $context->{'sections'} = \@sections; } ); # kate: syntax perl