| 1 | package Bot::Backbone::Service::Role::ChatConsumer; | ||
| 2 | $Bot::Backbone::Service::Role::ChatConsumer::VERSION = '0.160630'; | ||
| 3 | use v5.10; | ||
| 4 | use Moose::Role; | ||
| 5 | |||
| 6 | with 'Bot::Backbone::Service::Role::SendPolicy'; | ||
| 7 | |||
| 8 | # ABSTRACT: Role for services that listen for chat messages | ||
| 9 | |||
| 10 | |||
| 11 | has chat_name => ( | ||
| 12 | is => 'ro', | ||
| 13 | isa => 'Str', | ||
| 14 | required => 1, | ||
| 15 | init_arg => 'chat', | ||
| 16 | predicate => 'has_chat', | ||
| 17 | ); | ||
| 18 | |||
| 19 | |||
| 20 | has chat => ( | ||
| 21 | is => 'ro', | ||
| 22 | does => 'Bot::Backbone::Service::Role::Chat', | ||
| 23 | init_arg => undef, | ||
| 24 | lazy_build => 1, | ||
| 25 | weak_ref => 1, | ||
| 26 | |||
| 27 | # lazy_build implies (predicate => has_chat) | ||
| 28 | predicate => 'has_setup_the_chat', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # XXX Don't delegate these two. Delegation and -excludes don't seem to | ||
| 32 | # cooperate very well. | ||
| 33 | sub send_message { shift->chat->send_message(@_) } | ||
| 34 | sub send_reply { shift->chat->send_reply(@_) } | ||
| 35 | |||
| 36 | sub _build_chat { | ||
| 37 | my $self = shift; | ||
| 38 | my $chat = $self->bot->services->{ $self->chat_name }; | ||
| 39 | |||
| 40 | die "no such chat as ", $self->chat_name, "\n" | ||
| 41 | unless defined $chat; | ||
| 42 | |||
| 43 | return $chat; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | requires 'receive_message'; | ||
| 48 | |||
| 49 | |||
| 50 | before initialize => sub { | ||
| 51 | my $self = shift; | ||
| 52 | my $chat = $self->chat if $self->has_chat; | ||
| 53 | $chat->register_chat_consumer($self); | ||
| 54 | }; | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ | ||
| 59 | |||
| 60 | =pod | ||
| 61 | |||
| 62 | =encoding UTF-8 | ||
| 63 | |||
| 64 | =head1 NAME | ||
| 65 | |||
| 66 | Bot::Backbone::Service::Role::ChatConsumer - Role for services that listen for chat messages | ||
| 67 | |||
| 68 | =head1 VERSION | ||
| 69 | |||
| 70 | version 0.160630 | ||
| 71 | |||
| 72 | =head1 DESCRIPTION | ||
| 73 | |||
| 74 | Any service that needs to listen for chats and react to them will implement this | ||
| 75 | role. | ||
| 76 | |||
| 77 | =head1 ATTRIBUTES | ||
| 78 | |||
| 79 | =head2 chat_name | ||
| 80 | |||
| 81 | service my_responder => ( | ||
| 82 | service => '=My::Responder', | ||
| 83 | chat => 'jabber', | ||
| 84 | ); | ||
| 85 | |||
| 86 | This attribute is named C<chat> during construction. It is used to look up the | ||
| 87 | chat service set in L</chat>. | ||
| 88 | |||
| 89 | =head2 chat | ||
| 90 | |||
| 91 | This is the L<Bot::Backbone::Service::Role::Chat> that this responder will receive | ||
| 92 | messages from. | ||
| 93 | |||
| 94 | This must not be set directly and will be loaded lazily for you from the setting | ||
| 95 | in L</chat_name>. | ||
| 96 | |||
| 97 | =head1 REQUIRED METHODS | ||
| 98 | |||
| 99 | =head2 receive_message | ||
| 100 | |||
| 101 | $consumer->receive_message($message); | ||
| 102 | |||
| 103 | Whenever the chat service receives a message, it will pass it on to each | ||
| 104 | registered consumer by passing it to this method. | ||
| 105 | |||
| 106 | The method can do whatever it wants here and the return value of this method is | ||
| 107 | ignored. | ||
| 108 | |||
| 109 | =head1 METHODS | ||
| 110 | |||
| 111 | =head2 send_message | ||
| 112 | |||
| 113 | =head2 send_reply | ||
| 114 | |||
| 115 | Be sure to use these versions of the methods. Otherwise any send policies you have set on this class will be ignored. | ||
| 116 | |||
| 117 | =head2 initialize | ||
| 118 | |||
| 119 | This adds a bit of code to run before the service's C</initialize>. It loads | ||
| 120 | the chat object from the bot and registers this consumer with that chat service. | ||
| 121 | |||
| 122 | =head1 AUTHOR | ||
| 123 | |||
| 124 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 125 | |||
| 126 | =head1 COPYRIGHT AND LICENSE | ||
| 127 | |||
| 128 | This software is copyright (c) 2016 by Qubling Software LLC. | ||
| 129 | |||
| 130 | This is free software; you can redistribute it and/or modify it under | ||
| 131 | the same terms as the Perl 5 programming language system itself. | ||
| 132 | |||
| 133 | =cut |