# Type: Input and output # Category: Configuration # Description: Sets the main project of the user # Dialog: Hi -> OtherQuestions -> -> MainIssue -> MainActivity # Timeout: 5 mins # Fields: asked # Author: Andriy Lesyuk new Orangutan::Context( request => sub { my ($context, $user) = @_; $context->SetField('asked', 1); return Orangutan::Context::Random( 'What is the name of your primary project?', 'What is your main project you work most of your time on?' ); }, response => sub { my ($context, $user, $project) = @_; my @result = ( ); my @regexps = ( '(?:I(?:\'ve)? )?moved? to ["\']?(.+?)["\']?(?: project)?!*\.*', 'Now I(?:\'m| (?:am|work)) on ["\']?(.+?)["\']?(?: project)?!*\.*', 'Change(?: (?:the|my))? (?:main|primary|default) project to ["\']?(.+?)["\']?!*\.*', '(?:The )?name of(?: (?:the|my))? (?:main|primary|default) project is ["\']?(.+?)["\']?!*\.*', '(?:(?:My|The) )?(?:main|primary|default) project(?: I work most of(?: (?:my|the))? time on)? is ["\']?(.+?)["\']?!*\.*' ); if ($context->GetField('asked')) { push(@regexps, '(?:This|it|(?:(?:my|the) )?project) is ["\']?(.+?)["\']?!*\.*'); } my $matched = 0; foreach my $regexp (@regexps) { if ($project =~ /^$regexp$/i) { $project = $1; $matched++; last; } } if (($matched || $context->GetField('asked')) && ($project =~ /[a-z]+/i)) { push(@result, 1); push(@result, $project); } return @result; }, handler => sub { my ($context, $user, $item, $project) = @_; my $projects = $main::query->ProjectExists($project); if (defined($projects)) { if (ref $projects eq 'ARRAY') { $user->SendMessage( 'Wow!.. I found several projects with such name!.. '. 'I need to consult with my creator what should I do in such cases...' ); $main::users->TellCreator( "It happened! :) Redmine has several projects with the name \"$project\"... ". "Please don't forget to fix me!.." ); } elsif (ref $projects eq 'HASH') { if (!defined($user->GetProjectID) || ($user->GetProjectID != $projects->{'id'})) { $user->SetProject($projects->{'name'}); $user->SetProjectID($projects->{'id'}); if (defined($user->GetActivity)) { my $activityid = $main::query->ActivityExists($user->GetActivity, $projects->{'id'}); if ($activityid) { $user->SetActivityID($activityid); } else { $user->SetActivity; $user->SetActivityID; } } if (!defined($user->GetActivity)) { my $default_activity = $main::query->GetDefaultActivity($projects->{'id'}); if (defined($default_activity)) { $user->SetActivityID($default_activity->{'id'}); $user->SetActivity($default_activity->{'name'}); } } $main::config->Context('Ok')->Tell($user); $user->SetIssue; my $issue = $user->GetResponse('MainIssue'); if ($issue) { if ($context->GetField('asked')) { # only on initial configuration $issue->SetField('initial', 1); } $user->AddRequest($issue); } } else { $user->SendMessage([ "That's the project I'm using.", 'You have already told.', 'I know...' ]); } $context->UnsetField('asked'); $context->SetTimeout; } else { $user->SendMessage([ "Hm... I can't find such project... Please ensure that you used the correct name.", 'Looks like you made a mistake in the project name... Maybe you need to create it.', 'Perhaps this project is very new and was not added to Redmine yet. So please create it.' ]); } } else { $main::config->Context('NoDatabase')->Tell($user); } return 1; }, ontimeout => sub { my ($context, $user) = @_; if ($context->GetField('asked')) { $user->SendMessage([ 'Ok... Never mind.', 'Okay... You can tell it later...', 'Well... Just tell me what is your main project when you are ready.' ]); $context->UnsetField('asked'); } $context->SetTimeout; return 1; }, help => { title => 'How can I change default project?', question => '^(?:How )?(?:(?:can|do) I |to )(?:change|set|choose|select) '. '(?:(?:the|my) )?(?:default|main|primary) project\?*!*\.*$', answer => "If you do not specify a project when creating a task Orangutan uses the default project ". "(if was specified, of course). ". "You can change/set the default project anytime:\n". " o Change my main project to ClipSync\n". " o Now I work on PayPartners\n". " o My primary project is UPC\n". " o I moved to Kayako\n". "The name of the default project is asked in the initial configuration dialog...", weight => 510 }, timeout => 300, weight => [ undef, 90, undef ] ); # kate: syntax perl