| 1 | package Bot::Backbone::Service::Fact::Keyword; | ||
| 2 | { | ||
| 3 | $Bot::Backbone::Service::Fact::Keyword::VERSION = '0.142250'; | ||
| 4 | } | ||
| 5 | use Bot::Backbone::Service; | ||
| 6 | |||
| 7 | # ABSTRACT: Memorize keywords and respond to them when mentioned | ||
| 8 | |||
| 9 | with qw( | ||
| 10 | Bot::Backbone::Service::Role::Service | ||
| 11 | Bot::Backbone::Service::Role::Responder | ||
| 12 | Bot::Backbone::Service::Role::Storage | ||
| 13 | ); | ||
| 14 | |||
| 15 | |||
| 16 | service_dispatcher as { | ||
| 17 | command '!keyword' => given_parameters { | ||
| 18 | parameter 'keyword' => ( match => qr/.+/ ); | ||
| 19 | parameter 'response' => ( match_original => qr/.+/ ); | ||
| 20 | } run_this_method 'learn_keyword'; | ||
| 21 | |||
| 22 | command '!forget_keyword' => given_parameters { | ||
| 23 | parameter 'keyword' => ( match => qr/.+/ ); | ||
| 24 | parameter 'response' => ( match_original => qr/.+/ ); | ||
| 25 | } run_this_method 'forget_keyword'; | ||
| 26 | |||
| 27 | command '!forget_keyword' => given_parameters { | ||
| 28 | parameter 'keyword' => ( match => qr/.+/ ); | ||
| 29 | } run_this_method 'forget_keyword'; | ||
| 30 | |||
| 31 | also not_command spoken respond_by_method 'recall_keyword_sometimes'; | ||
| 32 | }; | ||
| 33 | |||
| 34 | |||
| 35 | has frequency => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'Num', | ||
| 38 | required => 1, | ||
| 39 | default => 0.1, | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | sub load_schema { | ||
| 44 | my ($self, $db_conn) = @_; | ||
| 45 | |||
| 46 | $db_conn->run(fixup => sub { | ||
| 47 | $_->do(q[ | ||
| 48 | CREATE TABLE IF NOT EXISTS fact_keywords( | ||
| 49 | keyword TEXT, | ||
| 50 | response TEXT, | ||
| 51 | PRIMARY KEY (keyword, response) | ||
| 52 | ) | ||
| 53 | ]); | ||
| 54 | }); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub learn_keyword { | ||
| 59 | my ($self, $message) = @_; | ||
| 60 | |||
| 61 | my $keyword = $message->parameters->{keyword}; | ||
| 62 | my $response = $message->parameters->{response}; | ||
| 63 | |||
| 64 | $self->db_conn->run(fixup => sub { | ||
| 65 | $_->do(q[ | ||
| 66 | INSERT OR IGNORE INTO fact_keywords(keyword, response) | ||
| 67 | VALUES (?, ?) | ||
| 68 | ], undef, $keyword, $response); | ||
| 69 | }); | ||
| 70 | |||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub forget_keyword { | ||
| 76 | my ($self, $message) = @_; | ||
| 77 | |||
| 78 | my $keyword = $message->parameters->{keyword}; | ||
| 79 | my $response = $message->parameters->{response}; | ||
| 80 | |||
| 81 | if ($response and $response =~ /\S/) { | ||
| 82 | $self->db_conn->run(fixup => sub { | ||
| 83 | $_->do(q[ | ||
| 84 | DELETE FROM fact_keywords | ||
| 85 | WHERE keyword = ? AND response = ? | ||
| 86 | ], undef, $keyword, $response); | ||
| 87 | }); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $self->db_conn->run(fixup => sub { | ||
| 91 | $_->do(q[ | ||
| 92 | DELETE FROM fact_keywords | ||
| 93 | WHERE keyword = ? | ||
| 94 | ], undef, $keyword); | ||
| 95 | }); | ||
| 96 | } | ||
| 97 | |||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub recall_keyword_sometimes { | ||
| 103 | my ($self, $message) = @_; | ||
| 104 | |||
| 105 | return unless rand() < $self->frequency; | ||
| 106 | |||
| 107 | my @words = map { $_->text } $message->all_args; | ||
| 108 | my $qlist = join ', ', ('?') x scalar @words; | ||
| 109 | my ($response) = $self->db_conn->run(fixup => sub { | ||
| 110 | $_->selectrow_array(qq[ | ||
| 111 | SELECT response | ||
| 112 | FROM fact_keywords | ||
| 113 | WHERE keyword IN ($qlist) | ||
| 114 | ORDER BY RANDOM() | ||
| 115 | LIMIT 1 | ||
| 116 | ], undef, @words); | ||
| 117 | }); | ||
| 118 | |||
| 119 | return unless $response; | ||
| 120 | return $response; | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | sub initialize { } | ||
| 125 | |||
| 126 | __PACKAGE__->meta->make_immutable; | ||
| 127 | |||
| 128 | __END__ |