Backbone/Service/Role/Responder.pm untested

filter: Statementstmt 0.0% 0 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 20 CC 1 SCAR 6.9
CC
1
Cov
0%
send_reply
2.0
send_message
2.0
BEGIN
2.0
CRAP 2.0
1 package Bot::Backbone::Service::Role::Responder;
2 $Bot::Backbone::Service::Role::Responder::VERSION = '0.160630';
3 use v5.10;
4 use Moose::Role;
5  
6 # ABSTRACT: A role for services that respond to messages
7  
8  
9 sub send_reply {
10 my ($self, $message, $options) = @_;
11  
12 $self->send_message({
13 chat => $message->chat,
14 group => $message->group,
15 to => $message->from->username,
16 %$options,
17 });
18 }
19  
20  
21 sub send_message {
22 my ($self, $options) = @_;
23  
24 my $chat = delete $options->{chat};
25  
26 $chat->send_message($options);
27 }
28  
29 1;
30  
31 __END__
32  
33 =pod
34  
35 =encoding UTF-8
36  
37 =head1 NAME
38  
39 Bot::Backbone::Service::Role::Responder - A role for services that respond to messages
40  
41 =head1 VERSION
42  
43 version 0.160630
44  
45 =head1 SYNOPSIS
46  
47 package MyBot::Service::Echo;
48 use v5.14; # because newer Perl is cooler than older Perl
49 use Bot::Backbone::Service;
50  
51 with qw(
52 Bot::Backbone::Service::Role::Service
53 Bot::Backbone::Service::Role::Responder
54 );
55  
56 # Instead of Bot::Backbone::Service::Role::Responder, you may prefer to
57 # apply the Bot::Backbone::Service::Role::ChatConsumer role instead. It
58 # really depends on if this module will be used across multiple chats or
59 # needs to be tied to a specific chat.
60  
61 service_dispatcher as {
62 command '!echo' => given_parameters {
63 parameter thing => ( match => qr/.+/ );
64 } respond_by_method 'echo_back';
65 };
66  
67 sub echo_back {
68 my ($self, $message) = @_;
69 return $message->parameters->{thing};
70 }
71  
72 __PACKAGE__->meta->make_immutable; # very good idea
73  
74 =head1 DESCRIPTION
75  
76 This role will provide implementations of C<send_message> and C<send_reply> methods appropriate if you build a service that just replies to input passed to it.
77  
78 =head1 METHODS
79  
80 =head2 send_reply
81  
82 $service->send_reply($message, \%options);
83  
84 This will send a reply back to the given message.
85  
86 =head2 send_message
87  
88 $service->send_message({
89 chat => $chat,
90 %options,
91 });
92  
93 This works just like your typical C<send_message> method, but it requires a C<chat> argument to tell it where to send the message. This is mostly useful if all you need to do is reply to whatever message was received from whichever source it was received from.
94  
95 =head1 AUTHOR
96  
97 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
98  
99 =head1 COPYRIGHT AND LICENSE
100  
101 This software is copyright (c) 2016 by Qubling Software LLC.
102  
103 This is free software; you can redistribute it and/or modify it under
104 the same terms as the Perl 5 programming language system itself.
105  
106 =cut