Skip to content

Commit

Permalink
Merge pull request #46 from miamibc/flip-plugin
Browse files Browse the repository at this point in the history
Flip plugin
  • Loading branch information
miamibc authored Dec 6, 2021
2 parents 32f1bc2 + 04acf09 commit 4cd0bb2
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 3 deletions.
1 change: 1 addition & 0 deletions joker.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
new Joker\Plugin\Server( ['host' => '127.0.0.1', 'port' => 5566] ),
new Joker\Plugin\Temp( ['api_key' => getenv( 'OPENWEATHER_API_KEY' ),'default' => 'Tallinn'] ),
new Joker\Plugin\Advice(),
new Joker\Plugin\Flip(),
new Joker\Plugin\Vkmusic(),
new Joker\Plugin\Ytmusic( ['api_key' => getenv('GOOGLE_API_KEY')]),
new Joker\Plugin\Spotify( ['client_id' => getenv( 'SPOTIFY_CLIENT_ID' ),'secret' =>getenv( 'SPOTIFY_SECRET' )] ),
Expand Down
8 changes: 8 additions & 0 deletions src/Parser/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public function answerMessage( $text, $options = [] )
return $this->bot()->sendMessage( $this->data['message']['chat']['id'], $text, $options );
}

public function replyMessage( $text, $options = [] )
{
if (!isset($this->data['message']['message_id'],
$this->data['message']['chat']['id'])) return false;
$options['reply_to_message_id'] = $this->data['message']['message_id'];
return $this->bot()->sendMessage( $this->data['message']['chat']['id'], $text, $options );
}

public function deleteMessage()
{
if (!isset($this->data['message']['chat']['id'], $this->data['message']['message_id'])) return false;
Expand Down
125 changes: 125 additions & 0 deletions src/Plugin/Flip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Flip plugin for Joker
*
* Flips text upside-down and back
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

namespace Joker\Plugin;

use Joker\Parser\Update;

class Flip extends Base
{

private static $table = [
"a" => "\u{0250}",
"b" => "q",
"c" => "\u{0254}",
"d" => "p",
"e" => "\u{01DD}",
"f" => "\u{025F}",
"g" => "\u{0183}",
"h" => "\u{0265}",
"i" => "\u{0131}",
"j" => "\u{027E}",
"k" => "\u{029E}",
//l : '\u0283",
"m" => "\u{026F}",
"M" => "W",
"n" => "u",
"r" => "\u{0279}",
"t" => "\u{0287}",
"v" => "\u{028C}",
"w" => "\u{028D}",
"y" => "\u{028E}",
"." => "\u{02D9}",
"[" => "]",
"(" => ")",
"{" => "}",
"?" => "\u{00BF}",
"!" => "\u{00A1}",
"'" => ",",
"<" => ">",
"_" => "\u{203E}",
"\u203F" => "\u{2040}",
"\u2045" => "\u{2046}",
"\u2234" => "\u{2235}",
"\r" => "\n",
"а" => "ɐ",
"б" => "ƍ",
"в" => "ʚ",
"г" => "ɹ",
"д" => "ɓ",
"ё" => "ǝ",
"е" => "ǝ",
"ж" => "ж",
"з" => "ε",
"и" => "и",
"й" => "ņ",
"к" => "ʞ",
"л" => "v",
"м" => "w",
"н" => "н",
"о" => "о",
"п" => "u",
"р" => "d",
"с" => "ɔ",
"т" => "ɯ", // ʟ ɯ ᅩ
"у" => "ʎ",
"ф" => "ф",
"х" => "х",
"ц" => "ǹ",
"ч" => "Һ",
"ш" => "m",
"щ" => "m",
"ъ" => "q",
"ы" => "ıq",
"ь" => "q",
"э" => "є",
"ю" => "",
"я" => "ʁ",
];

public function onPublicText( Update $update )
{
if ($update->message()->text()->trigger() === 'flip')
{
$text = $update->message()->text()->token(1);
$update->replyMessage( self::flip($text));
return false;
}
}

/**
* Flip text
* @param string $text
*
* @return string
*/
public static function flip($text) : string
{
// split unicode string
$text = preg_split('//u', mb_strtolower( $text ));

// flip with normal table
$result1 = array_map(function ($char){
return strtr($char, self::$table);
}, $text );

// flip with reversed+flipped table
$result2 = array_map(function ($char){
return strtr($char, array_reverse(array_flip(self::$table)));
}, $text );

// find best result
$best = count( array_diff($text, $result1) ) >= count(array_diff($text, $result2)) ? $result1 : $result2;

// reverse and return sting
return implode("", array_reverse($best));
}

}
6 changes: 3 additions & 3 deletions src/Plugin/Koldun.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ public function onPublicText( Update $update )
if (!in_array( $text->trigger(), $this->getOption('triggers'))) return;

$google = $this->client->getAsync('https://www.google.com/search?' . http_build_query(['q'=>(string)$text]) );
$update->bot()->console( "Google started");
$update->bot()->log( "Google started");
$google->then(
function (ResponseInterface $res) use ($update){
$update->bot()->console( "Google status " . $res->getStatusCode() );
$update->bot()->log( "Google status " . $res->getStatusCode() );
file_put_contents("data/google.html", $html = $res->getBody());
if (!preg_match('@<div class="\w+" data-attrid="wa:\/description".*<\/div>@iU', $html )) return;
$didom = new Document("<body>$html</body>");
$text = $didom->first('div[data-attrid="wa:/description"]')->text();
$update->answerMessage("Google said: $text");
},
function (RequestException $e) use ($update) {
$update->bot()->console( "Google error " . $e->getMessage() );
$update->bot()->log( "Google error " . $e->getMessage() );
}
);
/*
Expand Down
22 changes: 22 additions & 0 deletions src/Plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Plugins are well documented in inline comments, some interesting details will be
* [Cowsay Plugin](#cowsay-plugin)
* [Currency Plugin](#currency-plugin)
* [Excuse Plugin](#excuse-plugin)
* [Flip Plugin](#flip-plugin)
* [Forwarder Plugin](#forwarder-plugin)
* [Game Plugin](#game-plugin)
* [Hello Plugin](#hello-plugin)
Expand Down Expand Up @@ -234,6 +235,27 @@ Bot will answer something like

Ported from [lgg/excuse-generator](https://github.com/lgg/excuse-generator)

## Flip Plugin

Flips text upside-down and back.

```
Sergei Miami, [5/12/21 12:55 PM]
!flip мир перевернулся
Joker Test, [5/12/21 12:55 PM]
[In reply to Sergei Miami]
ʁɔvʎнdǝʚǝdǝu dиw
Sergei Miami, [5/12/21 12:55 PM]
!flip ʁɔvʎнdǝʚǝdǝu dиw
Joker Test, [5/12/21 12:55 PM]
[In reply to Sergei Miami]
мир перевернулся
```


## Forwarder Plugin

Forwards messages from one chat to another. Rules can be added with configuration, example:
Expand Down
29 changes: 29 additions & 0 deletions tests/Plugin/FlipTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

namespace Tests\Plugin;

use Joker\Plugin\Flip;
use PHPUnit\Framework\TestCase;

class FlipTest extends TestCase
{

public function testFlip()
{
$this->assertSame("ʁɔvʎнdǝʚǝdǝu dиw", Flip::flip("мир перевернулся"));
$this->assertSame("мир перевернулся", Flip::flip("ʁɔvʎнdǝʚǝdǝu dиw"));

$this->assertSame("˙ʇ,uop uoʎ ʇɐɥʍ uɹɐǝl ˙uɐɔ uoʎ ʇɐɥʍ ʍoɥs", Flip::flip("Show what you can. Learn what you don't."));
$this->assertSame("show whаt уoп сап. lеагп whаt уoп doп't.", Flip::flip("˙ʇ,uop uoʎ ʇɐɥʍ uɹɐǝl ˙uɐɔ uoʎ ʇɐɥʍ ʍoɥs"));

// @TODO don't lowercase
$this->assertSame('ʁɔvʎнdǝʚǝdǝu dиw', Flip::flip('Мир перевернулся'));


}
}

0 comments on commit 4cd0bb2

Please sign in to comment.