Compare commits
	
		
			1 Commits
		
	
	
		
			master
			...
			b4a5a915ba
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| b4a5a915ba | 
							
								
								
									
										134
									
								
								F1DataBot.pm
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								F1DataBot.pm
									
									
									
									
									
								
							| @@ -12,7 +12,6 @@ use Encode; | ||||
| use FindBin; | ||||
|  | ||||
| # CPAN packages | ||||
| use Cache::FastMmap; | ||||
| use JSON; | ||||
| use Log::Log4perl; | ||||
| use YAML; | ||||
| @@ -22,12 +21,6 @@ package F1DataBot; | ||||
|  | ||||
| # Constants and initalisations | ||||
| Log::Log4perl->init("$FindBin::Bin/log.conf"); | ||||
| my $CACHE = Cache::FastMmap->new( | ||||
|     share_file => '/tmp/f1_bot', | ||||
|     serializer => 'json', | ||||
|     unlink_on_exit => 0, | ||||
|     expire_time => 86400, | ||||
| ); | ||||
|  | ||||
| sub new { | ||||
|     my ( $Type, %Param ) = @_; | ||||
| @@ -48,13 +41,10 @@ sub new { | ||||
|     # load remembered update ids | ||||
|     $Self->{MessageIDs} = YAML::LoadFile("$FindBin::Bin/message_ids.yml"); | ||||
|  | ||||
|     # whitelist | ||||
|     $Self->{Whitelist} = $Param{Whitelist}; | ||||
|  | ||||
|     return $Self; | ||||
| } | ||||
|  | ||||
| =head1 greet | ||||
| =head1 Greet | ||||
|  | ||||
| 	Merely a dummy routine to test the bot's functionallity. Maybe using it for easter eggs or some kind of fun later. | ||||
|  | ||||
| @@ -193,28 +183,20 @@ sub statistics { | ||||
|             my $StatIdentifier = $+{statidentifier}; | ||||
|             given ($StatIdentifier) { | ||||
|                 when ('standings') { | ||||
|                     my $Standings; | ||||
|                     if ( $CACHE->get('driver_standings') ) { | ||||
|                         $Standings = $CACHE->get('driver_standings'); | ||||
|                     } | ||||
|                     else { | ||||
|                         $Standings = json POST( | ||||
|                             join( '/', | ||||
|                                 ( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) | ||||
|                             ), | ||||
|                             {} | ||||
|                         ); | ||||
|                         $CACHE->set('driver_standings', $Standings); | ||||
|                     } | ||||
|  | ||||
|                     my $Standings = json POST( | ||||
|                         join( '/', | ||||
|                             ( $Self->{URL}{Ergast}, 'current', 'driverStandings.json' ) | ||||
|                         ), | ||||
|                         {} | ||||
|                     ); | ||||
|                     my %DriverStandings; | ||||
|                     my $DriverStandingsFormatted = sprintf( "%2s %3s %-5s%7s\n", | ||||
|                     my $DriverStandingsFormatted = sprintf( "%-3s%-4s%-5s%7s\n", | ||||
|                         "#", "No.", "Code", "Points" ); | ||||
|                     for my $Driver ( $Standings->{MRData}->{StandingsTable} | ||||
|                         ->{StandingsLists}->[0]->{DriverStandings}->@* ) | ||||
|                     { | ||||
|                         $DriverStandingsFormatted .= sprintf( | ||||
|                             "%2d %3d %-5s%7d\n", | ||||
|                             "%-3d%-4d%-5s%7d\n", | ||||
|                             $Driver->{positionText}, | ||||
|                             $Driver->{Driver}->{permanentNumber}, | ||||
|                             $Driver->{Driver}->{code}, | ||||
| @@ -250,38 +232,30 @@ sub statistics { | ||||
|             my $StatIdentifier = $+{statidentifier}; | ||||
|             given ($StatIdentifier) { | ||||
|                 when ('standings') { | ||||
|                     my $Standings; | ||||
|                     if ( $CACHE->get('constructor_standings') ) { | ||||
|                         $Standings = $CACHE->get('constructor_standings'); | ||||
|                     } | ||||
|                     else { | ||||
|                         $Standings = json POST( | ||||
|                             join( '/', | ||||
|                                 ( $Self->{URL}{Ergast}, 'current', 'constructorStandings.json' ) | ||||
|                             ), | ||||
|                             {} | ||||
|                         ); | ||||
|                         $CACHE->set('constructor_standings', $Standings); | ||||
|                     } | ||||
|                     my $Standings = json POST( | ||||
|                         join( | ||||
|                             '/', | ||||
|                             ( | ||||
|                                 $Self->{URL}{Ergast}, 'current', | ||||
|                                 'constructorStandings.json' | ||||
|                             ) | ||||
|                         ), | ||||
|                         {} | ||||
|                     ); | ||||
|                     $Self->{LogObject} | ||||
|                       ->info( 'statistics: Fetched standings are ' | ||||
|                           . Dumper($Standings) ); | ||||
|                     my $ConstructorStandingsFormatted = sprintf( "%2s %-15s%7s\n", | ||||
|                         "#", "Name", "Points" ); | ||||
|                     my %ConstructorStandings; | ||||
|                     my $ConstructorStandingsFormatted = ''; | ||||
|                     for my $Constructor ( $Standings->{MRData}->{StandingsTable} | ||||
|                         ->{StandingsLists}->[0]->{ConstructorStandings}->@* ) | ||||
|                     { | ||||
|                         $ConstructorStandingsFormatted .= sprintf( | ||||
|                             "%2d %-15s%7d\n", | ||||
|                             $Constructor->{positionText}, | ||||
|                             $Constructor->{Constructor}->{name}, | ||||
|                             $Constructor->{points} | ||||
|                         ); | ||||
|                         $ConstructorStandingsFormatted .= | ||||
| "Position: $Constructor->{positionText}, Name: $Constructor->{Constructor}->{name} - Points: $Constructor->{points}\n"; | ||||
|                     } | ||||
|                     $ConstructorStandingsFormatted =~ s/^/<pre>/; | ||||
|                     $ConstructorStandingsFormatted =~ s/$/<\/pre>/; | ||||
|                     $ReturnData{text}         = $ConstructorStandingsFormatted; | ||||
|                     $ReturnData{parse_mode} = 'HTML'; | ||||
|                     $ReturnData{text} = $ConstructorStandingsFormatted; | ||||
|                 } | ||||
|                 default { | ||||
|                     $ReturnData{text} = | ||||
| @@ -315,38 +289,6 @@ sub statistics { | ||||
|  | ||||
| } | ||||
|  | ||||
| =head1 replyLoveQuote | ||||
|  | ||||
|     Reply with lovely text to special user id. | ||||
|  | ||||
| =cut | ||||
|  | ||||
| sub replyLoveQuote { | ||||
|  | ||||
|     my ( $Self, %Param ) = @_; | ||||
|  | ||||
|     return unless $Self->{Whitelist}->{$Param{Message}->{from}->{id}} eq 'Sarah' || $Self->{Whitelist}->{$Param{Message}->{from}->{id}} eq 'Stefan'; | ||||
|  | ||||
|     my $LoveQuotes = YAML::LoadFile("$FindBin::Bin/lovely_quotes.yml"); | ||||
|  | ||||
|     my %MessageData; | ||||
|     if ( ref $LoveQuotes eq 'ARRAY' ) { | ||||
|  | ||||
|         my $QuoteID = int( rand( $#{ $LoveQuotes } ) ); | ||||
|  | ||||
|         my $Message = $LoveQuotes->[$QuoteID]->{Text}; | ||||
|         $Message .= "\n\n - <i>$LoveQuotes->[$QuoteID]->{Source}</i>"; | ||||
|  | ||||
|         %MessageData = ( | ||||
|             text => $Message, | ||||
|             parse_mode => 'HTML', | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     return \%MessageData; | ||||
|  | ||||
| } | ||||
|  | ||||
| =head1 processMessage | ||||
|  | ||||
| 	Function which receives a single message and decides what to to based on message content and attributes. | ||||
| @@ -393,19 +335,12 @@ sub processMessage { | ||||
|     else { | ||||
|         $Self->{LogObject} | ||||
|           ->debug( 'Command not recognized. Data: ' . $Param{Message}->{message}->{text} ); | ||||
|         if ( $Self->{Whitelist}->{$Param{Message}->{message}->{from}->{id}} eq 'Sarah' | ||||
|             || $Self->{Whitelist}->{$Param{Message}->{message}->{from}->{id}} eq 'Stefan' ) { | ||||
|             $ResponseData = $Self->replyLoveQuote( Message => $Param{Message}->{message} ); | ||||
|         } | ||||
|         else { | ||||
|             $ResponseData->{text} = | ||||
|               "I'm sorry, " | ||||
|               . ( $Param{Message}->{message}->{chat}->{first_name} | ||||
|                 ? $Param{Message}->{message}->{chat}->{first_name} | ||||
|                 : $Param{Message}->{message}->{chat}->{username} ) | ||||
|               . ", I couldn't understand your request. Currently I can process the commands:\n\n\t\/greet\n\t\/statistics driver standings\n\t\/statistics constructor standings"; | ||||
|         } | ||||
|  | ||||
|         $ResponseData->{text} = | ||||
|           "I'm sorry, " | ||||
|           . ( $Param{Message}->{message}->{chat}->{first_name} | ||||
|             ? $Param{Message}->{message}->{chat}->{first_name} | ||||
|             : $Param{Message}->{message}->{chat}->{username} ) | ||||
|           . ", I couldn't understand your request. Currently I can process the commands:\n\n\t\/greet\n\t\/statistics driver standings\n\t\/statistics constructor standings"; | ||||
|     } | ||||
|  | ||||
|     my $ResponseResult = plain POST( | ||||
| @@ -454,13 +389,6 @@ sub fetchMessages { | ||||
|  | ||||
|     MESSAGE: | ||||
|     for my $Message (@Messages) { | ||||
|         if ( !$Self->{Whitelist}{$Message->{message}{from}{id}} ) { | ||||
|             $Self->{LogObject} | ||||
|               ->info( 'fetchMessages: User not whitelisted, skipping message ' . Dumper($MessageDataRaw) ); | ||||
|  | ||||
|             $Self->{MessageIDs}{$Message->{message}{message_id}} = 1; | ||||
|             next MESSAGE; | ||||
|         } | ||||
|         if ($Self->{MessageIDs}{$Message->{message}{message_id}}) { | ||||
|             $Self->{LogObject}->info('fetchMessages: Skipping known message_id'); | ||||
|             next MESSAGE; | ||||
| @@ -471,7 +399,7 @@ sub fetchMessages { | ||||
|             $Self->processMessage( Message => $Message, ); | ||||
|         } | ||||
|     } | ||||
|     YAML::DumpFile("$FindBin::Bin/message_ids.yml", $Self->{MessageIDs}); | ||||
|     YAML::DumpFile('message_ids.yml', $Self->{MessageIDs}); | ||||
| } | ||||
|  | ||||
| 1; | ||||
|   | ||||
| @@ -1,61 +0,0 @@ | ||||
| --- | ||||
| - | ||||
|   Text: "Du bist schöner als die Menschenkinder; Gnade ist ausgegossen über deine Lippen; darum hat Gott dich gesegnet auf ewig." | ||||
|   Source: "Psalm 45,3" | ||||
| - | ||||
|   Text: "Denn deine Liebe ist besser als Wein." | ||||
|   Source: "Hohelied 1,2b" | ||||
| - | ||||
|   Text: "Wie schön ist deine Liebe, meine Schwester, meine Braut; wie viel besser ist deine Liebe als Wein, und der Duft deiner Salben als alle Wohlgerüche!" | ||||
|   Source: "Hohelied 4,10" | ||||
| - | ||||
|   Text: "Denn die Liebe ist stark wie der Tod, und ihr Eifer unbezwinglich wie das Totenreich; ihre Glut ist Feuerglut, eine Flamme des Herrn. Große Wasser können die Liebe nicht auslöschen, und Ströme sie nicht ertränken." | ||||
|   Source: "Hohelied 8,6b-7a" | ||||
| - | ||||
|   Text: "Denn an Liebe habe ich Wohlgefallen und nicht am Opfern, an der Gotteserkenntnis mehr als an Brandopfern." | ||||
|   Source: "Hosea 6,6" | ||||
| - | ||||
|   Text: "Gleichwie mich der Vater liebt, so liebe ich euch; bleibt in meiner Liebe!" | ||||
|   Source: "Johannes 15,9" | ||||
| - | ||||
|   Text: "Wenn ich in Sprachen der Menschen und der Engel redete, aber keine Liebe hätte, so wäre ich tönendes Erz oder eine klingende Schelle." | ||||
|   Source: "1. Korinther 13,1" | ||||
| - | ||||
|   Text: "Und wenn ich Weissagung hätte und alle Geheimnisse wüsste und alle Erkenntnis, und wenn ich Glauben besäße, sodass ich Berge versetzte, aber keine Liebe hätte, so wäre ich nichts." | ||||
|   Source: "1. Korinther 13,2" | ||||
| - | ||||
|   Text: "Die Liebe ist langmütig und gütig, die Liebe beneidet nicht, die Liebe prahlt nicht, sie bläht sich nicht auf; sie ist nicht unanständig, sie sucht nicht das Ihre, sie lässt sich nicht erbittern, sie rechnet das Böse nicht zu; sie freut sich nicht an der Ungerechtigkeit, sie freut sich aber an der Wahrheit; sie erträgt alles, sie glaubt alles, sie hofft alles, sie erduldet alles." | ||||
|   Source: "1. Korinther 13,4-7" | ||||
| - | ||||
|   Text: "Lasst alles bei euch in Liebe geschehen!" | ||||
|   Source: "1. Korinther 16,14" | ||||
| - | ||||
|   Text: "Die Frucht des Geistes aber ist Liebe, Freude, Friede, Langmut, Freundlichkeit, Güte, Treue, Sanftmut, Selbstbeherrschung," | ||||
|   Source: "Galater 5,22" | ||||
| - | ||||
|   Text: "indem ihr mit aller Demut und Sanftmut, mit Langmut einander in Liebe ertragt" | ||||
|   Source: "Epheser 4,2" | ||||
| - | ||||
|   Text: "Über dies alles aber zieht die Liebe an, die das Band der Vollkommenheit ist." | ||||
|   Source: "Kolosser 3,14" | ||||
| - | ||||
|   Text: "Niemand verachte dich wegen deiner Jugend, sondern sei den Gläubigen ein Vorbild im Wort, im Wandel, in der Liebe, im Geist, im Glauben, in der Keuschheit!" | ||||
|   Source: "1. Timotheus 4,12" | ||||
| - | ||||
|   Text: "denn Gott hat uns nicht einen Geist der Furchtsamkeit gegeben, sondern der Kraft und der Liebe und der Zucht." | ||||
|   Source: "2. Timotheus 1,7" | ||||
| - | ||||
|   Text: "lasst uns aufeinander achtgeben, damit wir uns gegenseitig anspornen zur Liebe und zu guten Werken," | ||||
|   Source: "Hebräer 10,24" | ||||
| - | ||||
|   Text: "Vor allem aber habt innige Liebe untereinander, denn die Liebe wird eine Menge von Sünden zudecken." | ||||
|   Source: "1. Petrus 4,8" | ||||
| - | ||||
|   Text: "Geliebte, lasst uns einander lieben! Denn die Liebe ist aus Gott, und jeder, der liebt, ist aus Gott geboren und erkennt Gott." | ||||
|   Source: "1. Johannes 4,7" | ||||
| - | ||||
|   Text: "Geliebte, wenn Gott uns geliebt hat, so sind auch wir es schuldig, einander zu lieben." | ||||
|   Source: "1. Johannes 4,11" | ||||
| - | ||||
|   Text: "Und wir haben die Liebe erkannt und geglaubt, die Gott zu uns hat. Gott ist Liebe, und wer in der Liebe bleibt, der bleibt in Gott und Gott in ihm." | ||||
|   Source: "1. Johannes 4,16" | ||||
		Reference in New Issue
	
	Block a user