| 1 | package Bot::Backbone::Service::GroupChat; | ||
| 2 | $Bot::Backbone::Service::GroupChat::VERSION = '0.160630'; | ||
| 3 | use v5.10; | ||
| 4 | use Bot::Backbone::Service; | ||
| 5 | |||
| 6 | with qw( | ||
| 7 | Bot::Backbone::Service::Role::Service | ||
| 8 | Bot::Backbone::Service::Role::Dispatch | ||
| 9 | Bot::Backbone::Service::Role::Chat | ||
| 10 | ); | ||
| 11 | |||
| 12 | with 'Bot::Backbone::Service::Role::ChatConsumer' => { | ||
| 13 | -excludes => [ 'send_message' ], | ||
| 14 | }; | ||
| 15 | |||
| 16 | with_bot_roles qw( | ||
| 17 | Bot::Backbone::Bot::Role::GroupChat | ||
| 18 | ); | ||
| 19 | |||
| 20 | # ABSTRACT: A helper chat for performing group chats | ||
| 21 | |||
| 22 | |||
| 23 | has group => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Str', | ||
| 26 | required => 1, | ||
| 27 | ); | ||
| 28 | |||
| 29 | |||
| 30 | has nickname => ( | ||
| 31 | is => 'ro', | ||
| 32 | isa => 'Str', | ||
| 33 | predicate => 'has_nickname', | ||
| 34 | ); | ||
| 35 | |||
| 36 | |||
| 37 | sub initialize { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | my %options = ( | ||
| 41 | group => $self->group, | ||
| 42 | ); | ||
| 43 | |||
| 44 | $options{nickname} = $self->nickname if $self->has_nickname; | ||
| 45 | |||
| 46 | $self->chat->join_group(\%options); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub send_message { | ||
| 51 | my ($self, $params) = @_; | ||
| 52 | my $text = $params->{text}; | ||
| 53 | $self->chat->send_message({ | ||
| 54 | group => $self->group, | ||
| 55 | text => $text, | ||
| 56 | }); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub receive_message { | ||
| 61 | my ($self, $message) = @_; | ||
| 62 | |||
| 63 | return unless $message->is_group | ||
| 64 | and $message->group eq $self->group; | ||
| 65 | |||
| 66 | $self->resend_message($message); | ||
| 67 | $self->dispatch_message($message); | ||
| 68 | } | ||
| 69 | |||
| 70 | __PACKAGE__->meta->make_immutable; | ||
| 71 | |||
| 72 | __END__ | ||
| 73 | |||
| 74 | =pod | ||
| 75 | |||
| 76 | =encoding UTF-8 | ||
| 77 | |||
| 78 | =head1 NAME | ||
| 79 | |||
| 80 | Bot::Backbone::Service::GroupChat - A helper chat for performing group chats | ||
| 81 | |||
| 82 | =head1 VERSION | ||
| 83 | |||
| 84 | version 0.160630 | ||
| 85 | |||
| 86 | =head1 SYNOPSIS | ||
| 87 | |||
| 88 | service group_foo => ( | ||
| 89 | service => 'GroupChat', | ||
| 90 | chat => 'jabber_chat', | ||
| 91 | group => 'foo', | ||
| 92 | ); | ||
| 93 | |||
| 94 | =head1 DESCRIPTION | ||
| 95 | |||
| 96 | This is a chat consumer that provides chat services to a specific group on the | ||
| 97 | consumed chat service. | ||
| 98 | |||
| 99 | =head1 ATTRIBUTES | ||
| 100 | |||
| 101 | =head2 group | ||
| 102 | |||
| 103 | This is the name of the group this chat will communicate with. It will not | ||
| 104 | perform chats in any other group or directly. | ||
| 105 | |||
| 106 | =head2 nickname | ||
| 107 | |||
| 108 | This is the nickname to pass to the chat when joining the group. If not set, no | ||
| 109 | special nickname will be requested. | ||
| 110 | |||
| 111 | =head1 METHODS | ||
| 112 | |||
| 113 | =head2 initialize | ||
| 114 | |||
| 115 | Joins the L</group>. | ||
| 116 | |||
| 117 | =head2 send_message | ||
| 118 | |||
| 119 | Sends a message to the L</group>. | ||
| 120 | |||
| 121 | =head2 receive_message | ||
| 122 | |||
| 123 | If the message belongs to the L</group> this chat service works with, the | ||
| 124 | consumers will be notified and the dispatcher run. Otherwise, the message will | ||
| 125 | be ignored. | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 130 | |||
| 131 | =head1 COPYRIGHT AND LICENSE | ||
| 132 | |||
| 133 | This software is copyright (c) 2016 by Qubling Software LLC. | ||
| 134 | |||
| 135 | This is free software; you can redistribute it and/or modify it under | ||
| 136 | the same terms as the Perl 5 programming language system itself. | ||
| 137 | |||
| 138 | =cut |