Yuriy Zhilovets 4 years ago
parent
commit
96939f92d3
1 changed files with 23 additions and 1 deletions
  1. 23 1
      telegram.pl

+ 23 - 1
telegram.pl

@@ -16,6 +16,7 @@ use Data::Dumper;
 use Promises qw/deferred collect/;
 use Mojo::JSON qw/j/;
 use NetAddr::IP;
+use HTML::Restrict;
 
 my $NAME = "telegram";
 my $confdir = "config/".app->mode;
@@ -38,6 +39,22 @@ app->secrets(["Marsz, Marsz, Dabrowski"]);
 
 my $log = new Mojo::Log;
 
+# https://core.telegram.org/bots/api#formatting-options
+my $html_strip = HTML::Restrict->new(rules => {
+  b   => [],
+  strong => [],
+  i => [],
+  em => [],
+  u => [],
+  ins => [],
+  s => [],
+  strike => [],
+  del => [],
+  a => [qw/href/],
+  code => [qw/class/],
+  pre => [],
+});
+
 my $term;
 my $int;
 my $hup;
@@ -371,9 +388,14 @@ sub notify
   $params->{parse_mode} = "HTML" unless $rest->{parse_mode} && $rest->{parse_mode} eq "none";
   $params->{reply_to_message_id} = $rest->{msgid} if $rest->{msgid};
   $params->{disable_notification} = 1 if $rest->{silent};
-  
+
   my $disable_error_handler = delete $params->{disable_error_handler};
   
+  if ($params->{parse_mode} eq "HTML")
+  {
+    $params->{text} = $html_strip->process($params->{text});
+  }
+  
   my $promise = request("sendMessage", $params);
   unless ($disable_error_handler)
   {