| 1 | package Telebot::Plugin::Telegram::UI; | ||
| 2 | |||
| 3 | use Mojo::Base 'Mojolicious::Plugin', -signatures; | ||
| 4 | use Mojo::UserAgent; | ||
| 5 | use Carp 'croak'; | ||
| 6 | use Mojo::Util qw(camelize); | ||
| 7 | use Mojo::Collection qw(c); | ||
| 8 | use Mojo::JSON qw(to_json); | ||
| 9 | use Scalar::Util qw(blessed); | ||
| 10 | use Mojo::Loader qw(load_class load_classes); | ||
| 11 | |||
| 12 | has ['app'] => undef; | ||
| 13 | |||
| 14 | sub register { | ||
| 15 | my ($self, $app, $config) = @_; | ||
| 16 | |||
| 17 | $self->app($app); | ||
| 18 | |||
| 19 | # $app->tg->ui->input( | ||
| 20 | # chat_id => 1, | ||
| 21 | # [text => 'Field instruction',] | ||
| 22 | # [placeholder => 'Hint text (<=64 symbols)',] | ||
| 23 | # ); | ||
| 24 | $app->helper('tg.ui.input' => sub ($c, @args) { | ||
| 25 | my $options = _args(@args); | ||
| 26 | return { | ||
| 27 | ok => 0, | ||
| 28 | description => 'tg->ui->input: No chat_id', | ||
| 29 | } if !$options->{chat_id}; | ||
| 30 | $c->tg->request(sendMessage => { | ||
| 31 | chat_id => $options->{chat_id}, | ||
| 32 | text => $options->{text} // 'Input field', | ||
| 33 | reply_markup => { | ||
| 34 | force_reply => \1, | ||
| 35 | input_field_placeholder => $options->{placeholder} || '', | ||
| 36 | }, | ||
| 37 | }); | ||
| 38 | # save message_id for input field | ||
| 39 | }); | ||
| 40 | # $app->tg->ui->choice( | ||
| 41 | # chat_id => 1, | ||
| 42 | # [text => 'Choice instruction',] | ||
| 43 | # buttons => [ | ||
| 44 | # [ #Row1 | ||
| 45 | # { | ||
| 46 | # text => 'Button1', | ||
| 47 | # callback_data => 'btn1', | ||
| 48 | # }, | ||
| 49 | # { | ||
| 50 | # text => 'Button2', | ||
| 51 | # callback_data => 'btn2', | ||
| 52 | # }, | ||
| 53 | # ], | ||
| 54 | # [ #Row2 | ||
| 55 | # { | ||
| 56 | # text => 'Button3', | ||
| 57 | # callback_data => 'btn3', | ||
| 58 | # }, | ||
| 59 | # { | ||
| 60 | # text => 'Button4', | ||
| 61 | # callback_data => 'btn4', | ||
| 62 | # }, | ||
| 63 | # ] | ||
| 64 | # ], | ||
| 65 | # ); | ||
| 66 | $app->helper('tg.ui.choice' => sub ($c, @args) { | ||
| 67 | my $options = _args(@args); | ||
| 68 | return { | ||
| 69 | ok => 0, | ||
| 70 | description => 'tg->ui->choice: No chat_id', | ||
| 71 | } if !$options->{chat_id}; | ||
| 72 | return { | ||
| 73 | ok => 0, | ||
| 74 | description => 'tg->ui->choice: No buttons', | ||
| 75 | } if !@{$options->{buttons}||[]}; | ||
| 76 | $c->tg->request(sendMessage => { | ||
| 77 | chat_id => $options->{chat_id}, | ||
| 78 | text => $options->{text} // 'Select', | ||
| 79 | reply_markup => { | ||
| 80 | inline_keyboard => [map { | ||
| 81 | [map {$_} @$_] | ||
| 82 | } @{$options->{buttons}||[]}], | ||
| 83 | #resize_keyboard => \1, | ||
| 84 | #one_time_keyboard => \1, | ||
| 85 | }, | ||
| 86 | }); | ||
| 87 | }); | ||
| 88 | # $app->tg->ui->error( | ||
| 89 | # chat_id => 1, | ||
| 90 | # [text => 'Error text',] | ||
| 91 | # [reply_to => 1,] | ||
| 92 | # ); | ||
| 93 | $app->helper('tg.ui.error' => sub ($c, @args) { | ||
| 94 | my $options = _args(@args); | ||
| 95 | return { | ||
| 96 | ok => 0, | ||
| 97 | description => 'tg->ui->error: No chat_id', | ||
| 98 | } if !$options->{chat_id}; | ||
| 99 | $c->tg->request(sendMessage => { | ||
| 100 | chat_id => $options->{chat_id}, | ||
| 101 | text => $options->{text} || 'Error', | ||
| 102 | ($options->{reply_to} ? (reply_to_message_id => $options->{reply_to}) : ()) | ||
| 103 | }); | ||
| 104 | }); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | sub _args { | ||
| 109 | ~~@_%2==0 ? {@_} : $_[0]; | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =encoding utf8 | ||
| 117 | |||
| 118 | =head1 NAME | ||
| 119 | |||
| 120 | Telebot::Plugin::Telegram::UI - User intreface based on telegram API. | ||
| 121 | |||
| 122 | =head1 SYNOPSIS | ||
| 123 | |||
| 124 | $app->tg->ui->input( | ||
| 125 | chat_id => 111, | ||
| 126 | text => 'Fill your name', | ||
| 127 | placeholder => 'Your name here', | ||
| 128 | ); | ||
| 129 | |||
| 130 | $app->tg->ui->choice( | ||
| 131 | chat_id => 111, | ||
| 132 | [text => 'Select next action',] | ||
| 133 | buttons => [ | ||
| 134 | [ | ||
| 135 | { | ||
| 136 | text => 'Open beer', | ||
| 137 | callback_data => 'beer', | ||
| 138 | }, | ||
| 139 | { | ||
| 140 | text => 'Code something', | ||
| 141 | callback_data => 'waste_time', | ||
| 142 | }, | ||
| 143 | ], | ||
| 144 | [ | ||
| 145 | { | ||
| 146 | text => 'Drink beer', | ||
| 147 | callback_data => 'pleasure', | ||
| 148 | }, | ||
| 149 | { | ||
| 150 | text => 'Buy iPhone 115', | ||
| 151 | callback_data => 'waste_money', | ||
| 152 | }, | ||
| 153 | ] | ||
| 154 | ], | ||
| 155 | ); | ||
| 156 | |||
| 157 | $app->tg->ui->error( | ||
| 158 | chat_id => 111, | ||
| 159 | text => 'Something unexpected', | ||
| 160 | reply_to => 777, | ||
| 161 | ); | ||
| 162 | |||
| 163 | =head1 DESCRIPTION | ||
| 164 | |||
| 165 | L<Telebot::Plugin::Telegram::UI> - plugin defines some helpers | ||
| 166 | which will be more high level replacement of direct calls of | ||
| 167 | Telegram API. | ||
| 168 | |||
| 169 | B<IMPORTANT NOTE>. These helpers are EXPERIMENTAL. I'm not sure | ||
| 170 | they will not be changed or removed in future versions. | ||
| 171 | |||
| 172 | =head1 HELPERS | ||
| 173 | |||
| 174 | L<Telebot::Plugin::Telegram::UI> implements the following helpers. | ||
| 175 | |||
| 176 | =head2 tg->ui->input | ||
| 177 | |||
| 178 | Send to chat message which looks like input field. | ||
| 179 | |||
| 180 | =head2 tg->ui->choice | ||
| 181 | |||
| 182 | Send to chat message which looks like set of buttons with single choice. | ||
| 183 | |||
| 184 | =head2 tg->ui->error | ||
| 185 | |||
| 186 | Inform user about error in the Telegram chat. | ||
| 187 | |||
| 188 | =head1 METHODS | ||
| 189 | |||
| 190 | L<Telebot::Plugin::Telegram::UI> inherits all methods from L<Mojolicious::Plugin> and implements the following new ones. | ||
| 191 | |||
| 192 | =head2 register | ||
| 193 | |||
| 194 | my $tg = $plugin->register(Mojolicious->new); | ||
| 195 | |||
| 196 | Register plugin in Mojolicious application and define helpers. | ||
| 197 | |||
| 198 | =head1 COPYRIGHT AND LICENSE | ||
| 199 | |||
| 200 | Copyright (C) 2022, Igor Lobanov. | ||
| 201 | This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version | ||
| 202 | 2.0. | ||
| 203 | |||
| 204 | =head1 SEE ALSO | ||
| 205 | |||
| 206 | L<https://github.com/igorlobanov/telebot>, L<Mojolicious::Guides>, L<https://mojolicious.org>, | ||
| 207 | L<https://core.telegram.org/bots/api>. | ||
| 208 | |||
| 209 | =cut |