over engineered bots a little more.

This commit is contained in:
Hugo Levy-Falk 2020-04-11 20:59:07 +02:00
parent 08739ec0b7
commit cc05c9521b
14 changed files with 232 additions and 121 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
.env .env
*.log *.log*

View file

@ -10,6 +10,8 @@ python-telegram-bot = "*"
toml = "*" toml = "*"
ipython = "*" ipython = "*"
bpython = "*" bpython = "*"
requests = "*"
beautifulsoup4 = "*"
[requires] [requires]
python_version = "3.6" python_version = "3.6"

150
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "0cefd20d811ec52fdd5347a47a7ac75dba2afb82d1982af83b983ab33611d7f2" "sha256": "964c5cf86914a0a6193b9362c4acf3c38aae2c8547959a53810c87d23a9ef3da"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -23,6 +23,15 @@
], ],
"version": "==0.1.0" "version": "==0.1.0"
}, },
"beautifulsoup4": {
"hashes": [
"sha256:594ca51a10d2b3443cbac41214e12dbb2a1cd57e1a7344659849e2e20ba6a8d8",
"sha256:a4bbe77fd30670455c5296242967a123ec28c37e9702a8a81bd2f20a4baf0368",
"sha256:d4e96ac9b0c3a6d3f0caae2e4124e6055c5dcafde8e2f831ff194c104f0775a0"
],
"index": "pypi",
"version": "==4.9.0"
},
"blessings": { "blessings": {
"hashes": [ "hashes": [
"sha256:98e5854d805f50a5b58ac2333411b0482516a8210f23f43308baeb58d77c157d", "sha256:98e5854d805f50a5b58ac2333411b0482516a8210f23f43308baeb58d77c157d",
@ -33,18 +42,18 @@
}, },
"bpython": { "bpython": {
"hashes": [ "hashes": [
"sha256:56cc20dbe568c98c81de4990fddf5862c0d8d3ab0ad1cf7057988abc5f7686c2", "sha256:476ce09a896c4d34bf5e56aca64650c56fdcfce45781a20dc1521221df8cc49c",
"sha256:c7c6de7309311fd607d6cb47ef7e2d6e065d0a299199d51220d57732850a3efa" "sha256:95d95783bfadfa0a25300a648de5aba4423b0ee76b034022a81dde2b5e853c00"
], ],
"index": "pypi", "index": "pypi",
"version": "==0.18" "version": "==0.19"
}, },
"certifi": { "certifi": {
"hashes": [ "hashes": [
"sha256:017c25db2a153ce562900032d5bc68e9f191e44e9a0f762f373977de9df1fbb3", "sha256:1d987a998c75633c40847cc966fcf5904906c920a7f17ef374f5aa4282abd304",
"sha256:25b64c7da4cd7479594d035c08c2d809eb4aab3a26e5a990ea98cc450c320f1f" "sha256:51fcb31174be6e6664c5f69e3e1691a2d72a1a12e90f872cbdb1567eb47b6519"
], ],
"version": "==2019.11.28" "version": "==2020.4.5.1"
}, },
"cffi": { "cffi": {
"hashes": [ "hashes": [
@ -88,29 +97,27 @@
}, },
"cryptography": { "cryptography": {
"hashes": [ "hashes": [
"sha256:02079a6addc7b5140ba0825f542c0869ff4df9a69c360e339ecead5baefa843c", "sha256:0cacd3ef5c604b8e5f59bf2582c076c98a37fe206b31430d0cd08138aff0986e",
"sha256:1df22371fbf2004c6f64e927668734070a8953362cd8370ddd336774d6743595", "sha256:192ca04a36852a994ef21df13cca4d822adbbdc9d5009c0f96f1d2929e375d4f",
"sha256:369d2346db5934345787451504853ad9d342d7f721ae82d098083e1f49a582ad", "sha256:19ae795137682a9778892fb4390c07811828b173741bce91e30f899424b3934d",
"sha256:3cda1f0ed8747339bbdf71b9f38ca74c7b592f24f65cdb3ab3765e4b02871651", "sha256:1b9b535d6b55936a79dbe4990b64bb16048f48747c76c29713fea8c50eca2acf",
"sha256:44ff04138935882fef7c686878e1c8fd80a723161ad6a98da31e14b7553170c2", "sha256:2a2ad24d43398d89f92209289f15265107928f22a8d10385f70def7a698d6a02",
"sha256:4b1030728872c59687badcca1e225a9103440e467c17d6d1730ab3d2d64bfeff", "sha256:3be7a5722d5bfe69894d3f7bbed15547b17619f3a88a318aab2e37f457524164",
"sha256:58363dbd966afb4f89b3b11dfb8ff200058fbc3b947507675c19ceb46104b48d", "sha256:49870684da168b90110bbaf86140d4681032c5e6a2461adc7afdd93be5634216",
"sha256:6ec280fb24d27e3d97aa731e16207d58bd8ae94ef6eab97249a2afe4ba643d42", "sha256:587f98ce27ac4547177a0c6fe0986b8736058daffe9160dcf5f1bd411b7fbaa1",
"sha256:7270a6c29199adc1297776937a05b59720e8a782531f1f122f2eb8467f9aab4d", "sha256:5aca6f00b2f42546b9bdf11a69f248d1881212ce5b9e2618b04935b87f6f82a1",
"sha256:73fd30c57fa2d0a1d7a49c561c40c2f79c7d6c374cc7750e9ac7c99176f6428e", "sha256:6b744039b55988519cc183149cceb573189b3e46e16ccf6f8c46798bb767c9dc",
"sha256:7f09806ed4fbea8f51585231ba742b58cbcfbfe823ea197d8c89a5e433c7e912", "sha256:6b91cab3841b4c7cb70e4db1697c69f036c8bc0a253edc0baa6783154f1301e4",
"sha256:90df0cc93e1f8d2fba8365fb59a858f51a11a394d64dbf3ef844f783844cc793", "sha256:7598974f6879a338c785c513e7c5a4329fbc58b9f6b9a6305035fca5b1076552",
"sha256:971221ed40f058f5662a604bd1ae6e4521d84e6cad0b7b170564cc34169c8f13", "sha256:7a279f33a081d436e90e91d1a7c338553c04e464de1c9302311a5e7e4b746088",
"sha256:a518c153a2b5ed6b8cc03f7ae79d5ffad7315ad4569b2d5333a13c38d64bd8d7", "sha256:95e1296e0157361fe2f5f0ed307fd31f94b0ca13372e3673fa95095a627636a1",
"sha256:b0de590a8b0979649ebeef8bb9f54394d3a41f66c5584fff4220901739b6b2f0", "sha256:9fc9da390e98cb6975eadf251b6e5fa088820141061bf041cd5c72deba1dc526",
"sha256:b43f53f29816ba1db8525f006fa6f49292e9b029554b3eb56a189a70f2a40879", "sha256:cc20316e3f5a6b582fc3b029d8dc03aabeb645acfcb7fc1d9848841a33265748",
"sha256:d31402aad60ed889c7e57934a03477b572a03af7794fa8fb1780f21ea8f6551f", "sha256:d1bf5a1a0d60c7f9a78e448adcb99aa101f3f9588b16708044638881be15d6bc",
"sha256:de96157ec73458a7f14e3d26f17f8128c959084931e8997b9e655a39c8fde9f9", "sha256:ed1d0760c7e46436ec90834d6f10477ff09475c692ed1695329d324b2c5cd547",
"sha256:df6b4dca2e11865e6cfbfb708e800efb18370f5a46fd601d3755bc7f85b3a8a2", "sha256:ef9a55013676907df6c9d7dd943eb1770d014f68beaa7e73250fb43c759f4585"
"sha256:ecadccc7ba52193963c0475ac9f6fa28ac01e01349a2ca48509667ef41ffd2cf",
"sha256:fb81c17e0ebe3358486cd8cc3ad78adbae58af12fc2bf2bc0bb84e8090fa5ce8"
], ],
"version": "==2.8" "version": "==2.9"
}, },
"curtsies": { "curtsies": {
"hashes": [ "hashes": [
@ -121,10 +128,10 @@
}, },
"decorator": { "decorator": {
"hashes": [ "hashes": [
"sha256:54c38050039232e1db4ad7375cfce6748d7b41c29e95a081c8a6d2c30364a2ce", "sha256:41fa54c2a0cc4ba648be4fd43cff00aedf5b9465c9bf18d64325bc225f08f760",
"sha256:5d19b92a3c8f7f101c8dd86afd86b0f061a8ce4540ab8cd401fa2542756bce6d" "sha256:e3a62f0520172440ca0dcc823749319382e377f37f140a0b99ef45fecb84bfe7"
], ],
"version": "==4.4.1" "version": "==4.4.2"
}, },
"future": { "future": {
"hashes": [ "hashes": [
@ -161,18 +168,18 @@
}, },
"idna": { "idna": {
"hashes": [ "hashes": [
"sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407", "sha256:7588d1c14ae4c77d74036e8c22ff447b26d0fde8f007354fd48a7814db15b7cb",
"sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c" "sha256:a068a21ceac8a4d63dbfd964670474107f541babbd2250d61922f029858365fa"
], ],
"version": "==2.8" "version": "==2.9"
}, },
"ipython": { "ipython": {
"hashes": [ "hashes": [
"sha256:d9459e7237e2e5858738ff9c3e26504b79899b58a6d49e574d352493d80684c6", "sha256:ca478e52ae1f88da0102360e57e528b92f3ae4316aabac80a2cd7f7ab2efb48a",
"sha256:f6689108b1734501d3b59c84427259fd5ac5141afe2e846cfa8598eb811886c9" "sha256:eb8d075de37f678424527b5ef6ea23f7b80240ca031c2dd6de5879d687a65333"
], ],
"index": "pypi", "index": "pypi",
"version": "==7.12.0" "version": "==7.13.0"
}, },
"ipython-genutils": { "ipython-genutils": {
"hashes": [ "hashes": [
@ -190,10 +197,10 @@
}, },
"parso": { "parso": {
"hashes": [ "hashes": [
"sha256:56b2105a80e9c4df49de85e125feb6be69f49920e121406f15e7acde6c9dfc57", "sha256:0c5659e0c6eba20636f99a04f469798dca8da279645ce5c387315b2c23912157",
"sha256:951af01f61e6dccd04159042a0706a31ad437864ec6e25d0d7a96a9fbb9b0095" "sha256:8515fc12cfca6ee3aa59138741fc5624d62340c97e401c74875769948d4f2995"
], ],
"version": "==0.6.1" "version": "==0.6.2"
}, },
"pexpect": { "pexpect": {
"hashes": [ "hashes": [
@ -212,10 +219,10 @@
}, },
"prompt-toolkit": { "prompt-toolkit": {
"hashes": [ "hashes": [
"sha256:a402e9bf468b63314e37460b68ba68243d55b2f8c4d0192f85a019af3945050e", "sha256:563d1a4140b63ff9dd587bda9557cffb2fe73650205ab6f4383092fb882e7dc8",
"sha256:c93e53af97f630f12f5f62a3274e79527936ed466f038953dfa379d4941f651a" "sha256:df7e9e63aea609b1da3a65641ceaf5bc7d05e0a04de5bd45d05dbeffbabf9e04"
], ],
"version": "==3.0.3" "version": "==3.0.5"
}, },
"ptyprocess": { "ptyprocess": {
"hashes": [ "hashes": [
@ -226,31 +233,33 @@
}, },
"pycparser": { "pycparser": {
"hashes": [ "hashes": [
"sha256:a988718abfad80b6b157acce7bf130a30876d27603738ac39f140993246b25b3" "sha256:2d475327684562c3a96cc71adf7dc8c4f0565175cf86b6d7a404ff4c771f15f0",
"sha256:7582ad22678f0fcd81102833f60ef8d0e57288b6b5fb00323d101be910e35705"
], ],
"version": "==2.19" "version": "==2.20"
}, },
"pygments": { "pygments": {
"hashes": [ "hashes": [
"sha256:2a3fe295e54a20164a9df49c75fa58526d3be48e14aceba6d6b1e8ac0bfd6f1b", "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44",
"sha256:98c8aa5a9f778fcd1026a17361ddaf7330d1b7c62ae97c3bb0ae73e0b9b6b0fe" "sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324"
], ],
"version": "==2.5.2" "version": "==2.6.1"
}, },
"python-telegram-bot": { "python-telegram-bot": {
"hashes": [ "hashes": [
"sha256:0a97cbca638f949582b4ee326170d2f8d7f4bf559a4e511132bb2203623e04ad", "sha256:935397390910291c8e41d7f2a06a3bb13d1d74d78b59562be9f8a330d4527049",
"sha256:d3cffd020af4094d07c11783f875e5c682072ba7f5bc21ce89ff0222f4e6d742" "sha256:c7bdb5788ad2edea5c5c1bc8e50967fad68aa35245c209baadf74fc8ad00ff06"
], ],
"index": "pypi", "index": "pypi",
"version": "==12.4.2" "version": "==12.6.1"
}, },
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:11e007a8a2aa0323f5a921e9e6a2d7e4e67d9877e85773fba9ba6419025cbeb4", "sha256:43999036bfa82904b6af1d99e4882b560e5e2c68e5c4b0aa03b655f3d7d73fee",
"sha256:9cf5292fcd0f598c671cfc1e0d7d1a7f13bb8085e9a590f48c010551dc6c4b31" "sha256:b3f43d496c6daba4493e7c431722aeb7dbc6288f52a6e04e7b6023b0247817e6"
], ],
"version": "==2.22.0" "index": "pypi",
"version": "==2.23.0"
}, },
"six": { "six": {
"hashes": [ "hashes": [
@ -259,6 +268,13 @@
], ],
"version": "==1.14.0" "version": "==1.14.0"
}, },
"soupsieve": {
"hashes": [
"sha256:e914534802d7ffd233242b785229d5ba0766a7f487385e3f714446a07bf540ae",
"sha256:fcd71e08c0aee99aca1b73f45478549ee7e7fc006d51b37bec9e9def7dc22b69"
],
"version": "==2.0"
},
"toml": { "toml": {
"hashes": [ "hashes": [
"sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c", "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c",
@ -269,15 +285,17 @@
}, },
"tornado": { "tornado": {
"hashes": [ "hashes": [
"sha256:349884248c36801afa19e342a77cc4458caca694b0eda633f5878e458a44cb2c", "sha256:0fe2d45ba43b00a41cd73f8be321a44936dc1aba233dee979f17a042b83eb6dc",
"sha256:398e0d35e086ba38a0427c3b37f4337327231942e731edaa6e9fd1865bbd6f60", "sha256:22aed82c2ea340c3771e3babc5ef220272f6fd06b5108a53b4976d0d722bcd52",
"sha256:4e73ef678b1a859f0cb29e1d895526a20ea64b5ffd510a2307b5998c7df24281", "sha256:2c027eb2a393d964b22b5c154d1a23a5f8727db6fda837118a776b29e2b8ebc6",
"sha256:559bce3d31484b665259f50cd94c5c28b961b09315ccd838f284687245f416e5", "sha256:5217e601700f24e966ddab689f90b7ea4bd91ff3357c3600fa1045e26d68e55d",
"sha256:abbe53a39734ef4aba061fca54e30c6b4639d3e1f59653f0da37a0003de148c7", "sha256:5618f72e947533832cbc3dec54e1dffc1747a5cb17d1fd91577ed14fa0dc081b",
"sha256:c845db36ba616912074c5b1ee897f8e0124df269468f25e4fe21fe72f6edd7a9", "sha256:5f6a07e62e799be5d2330e68d808c8ac41d4a259b9cea61da4101b83cb5dc673",
"sha256:c9399267c926a4e7c418baa5cbe91c7d1cf362d505a1ef898fde44a07c9dd8a5" "sha256:c58d56003daf1b616336781b26d184023ea4af13ae143d9dda65e31e534940b9",
"sha256:c952975c8ba74f546ae6de2e226ab3cc3cc11ae47baf607459a6728585bb542a",
"sha256:c98232a3ac391f5faea6821b53db8db461157baa788f5d6222a193e9456e1740"
], ],
"version": "==6.0.3" "version": "==6.0.4"
}, },
"traitlets": { "traitlets": {
"hashes": [ "hashes": [
@ -295,10 +313,10 @@
}, },
"wcwidth": { "wcwidth": {
"hashes": [ "hashes": [
"sha256:8fd29383f539be45b20bd4df0dc29c20ba48654a41e661925e612311e9f3c603", "sha256:cafe2186b3c009a04067022ce1dcd79cb38d8d65ee4f4791b8888d6599d1bbe1",
"sha256:f28b3e8a6483e5d49e7f8949ac1a78314e740333ae305b4ba5defd3e74fb37a8" "sha256:ee73862862a156bf77ff92b09034fc4825dd3af9cf81bc5b360668d425f3c5f1"
], ],
"version": "==0.1.8" "version": "==0.1.9"
} }
}, },
"develop": {} "develop": {}

View file

@ -9,7 +9,7 @@ min_time = 3
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [] quotes = []
python = [ python = [
'klafytg.bots.chuck_norris.on_ping' 'klafytg.bots.chuck_norris.on_ping'

View file

@ -13,7 +13,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"Les ordinateurs, c'est comme les galères romaines : moins il y a de RAM, et plus ça rame !", "Les ordinateurs, c'est comme les galères romaines : moins il y a de RAM, et plus ça rame !",
"Il pédale dans la choucroute !", "Il pédale dans la choucroute !",

View file

@ -13,7 +13,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"C'est très simple.", "C'est très simple.",
"C'est facile.", "C'est facile.",

View file

@ -0,0 +1,38 @@
[bot]
name = "Klafyvel"
# Name of the environment variable to fetch
token = "KLAFYTOKEN"
# time between interventions in seconds
min_time = 3
# If channel is specified, only specified channels are authorized.
# channels = []
admins = [
452970435 # klafyvel
]
inline = "attack"
[cmd.ping]
quotes = [
"Parti me baigner dans le sang de nouveaux nés avec mes potes communistes, déso.",
"Je te foutrais tout ça au goulag.",
"Y a plus qu'à nationaliser.",
"Hop un salaire universel.",
"J'arrive pas à savoir si être de droite rend con ou si c'est juste un pré-requis.",
"Un laser, c'est assez simple: vous voyez des coconuts dans une piscine ? c'est déjà un bon début.",
"Un soliton c'est pas bien compliqué. Ça revient à faire courir un gros, un sportif et un moyen sur un matelas géant. Oui on dirait le début d'une blague offensante. Non les solitons ne sont pas offensants.",
"Macron démission.",
"Le mérite est une construction sociale.",
]
[match]
[join]
[cmd.attack]
python = [
'klafytg.bots.klafyvel.on_attack'
]
[cmd.help]
quotes = [
"I don't do much, but I can /attack 😏"
]
[cmd.start]
quotes = [
"I don't do much, but I can /attack 😏"
]

View file

@ -13,7 +13,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"PARCE QUE C'EST NOTRE PROJEEEET !", "PARCE QUE C'EST NOTRE PROJEEEET !",
"C'est de la poudre de perlimpinpin.", "C'est de la poudre de perlimpinpin.",

View file

@ -13,7 +13,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"En parlant de ça, Centrale ne sait vraiment pas travailler en multicampus", "En parlant de ça, Centrale ne sait vraiment pas travailler en multicampus",
"On avait ouvert une voie d'escalade sur une des façades de la résidence", "On avait ouvert une voie d'escalade sur une des façades de la résidence",

View file

@ -13,7 +13,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"Ça me désole quand munme.", "Ça me désole quand munme.",
"Le monde va mal, vous savez. Faites HEC.", "Le monde va mal, vous savez. Faites HEC.",

View file

@ -14,7 +14,7 @@ channels = [
admins = [ admins = [
452970435 # klafyvel 452970435 # klafyvel
] ]
[ping] [cmd.ping]
quotes = [ quotes = [
"Arcas:`à viarezo ils font des caleçons.` Chibrac:`bah non au rezo, à viarezo ils ont pas de boules à couvrir`", "Arcas:`à viarezo ils font des caleçons.` Chibrac:`bah non au rezo, à viarezo ils ont pas de boules à couvrir`",
"Les lentilles c'est extrêmement pratique de base pour toute personne active [wink-wink Chibrac, NDLR]", "Les lentilles c'est extrêmement pratique de base pour toute personne active [wink-wink Chibrac, NDLR]",

View file

@ -1,14 +1,13 @@
[Unit] [Unit]
Description=Klafyirc bots Description=Klafyvel telegram bots.
After=network-online.target After=network.target
[Service] [Service]
User=klafyvel
Restart=always
Type=simple Type=simple
WorkingDirectory=/usr/local/klafytg
User=root ExecStart=/usr/local/bin/pipenv run python -m /usr/local/klafytg/klafytg
Group=root
ExecStart=/usr/bin/python3 -m klafirc
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View file

@ -31,28 +31,62 @@ class Bot:
for k in self.config["match"].keys(): for k in self.config["match"].keys():
self.add_reaction(k, self.config["match"][k]) self.add_reaction(k, self.config["match"][k])
self.pings = []
for quote in self.config["ping"].get("quotes", []):
self.add_ping(quote)
for python in self.config["ping"].get("python", []):
self.add_python_ping(python)
for channel in self.config["bot"].get("channels", []): for channel in self.config["bot"].get("channels", []):
self.channels[int(channel)] = datetime.datetime(1,1,1) self.channels[int(channel)] = datetime.datetime(1,1,1)
self.all_channel_allowed = not self.config["bot"].get("channels", []) self.all_channel_allowed = not self.config["bot"].get("channels", [])
self.inline = self.config["bot"].get("inline", "ping")
self.commands = {}
self.updater = Updater(token=os.environ[self.config["bot"]["token"]], use_context=True) self.updater = Updater(token=os.environ[self.config["bot"]["token"]], use_context=True)
self.dispatcher = self.updater.dispatcher self.dispatcher = self.updater.dispatcher
self.quote_handler = CommandHandler('ping', self.on_ping) for command in self.config.get("cmd", {}).keys():
self.create_cmd(command)
handler = CommandHandler(command, getattr(self, "on_"+command))
self.dispatcher.add_handler(handler)
self.match_handler = MessageHandler(Filters.text, self.on_message) self.match_handler = MessageHandler(Filters.text, self.on_message)
self.inline_quote_handler = InlineQueryHandler(self.on_inline) self.inline_quote_handler = InlineQueryHandler(self.on_inline)
self.dispatcher.add_handler(self.inline_quote_handler) self.dispatcher.add_handler(self.inline_quote_handler)
self.dispatcher.add_handler(self.quote_handler)
self.dispatcher.add_handler(self.match_handler) self.dispatcher.add_handler(self.match_handler)
def create_cmd(self, name):
self.commands[name] = []
for quote in self.config["cmd"][name].get("quotes", []):
self.commands[name].append(quote)
for python in self.config["cmd"][name].get("python", []):
self.commands[name].append(self.fetch_callback(python))
def fun(update, context):
if not self.is_allowed(update, context):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="Sorry, mamma told me not to talk with random strangers. You need to allow chat id {}.".format(update.effective_chat.id),
reply_to_message_id=update.effective_message.message_id
)
return
quote = random.choice(self.commands[name])
if callable(quote):
quote = quote(self, update, context)
else:
c = {
"channel": update.effective_chat.title,
"name": self.name,
"user": update.effective_user.name,
"message": update.effective_message.text,
}
quote = quote.format(**c)
if quote:
context.bot.send_message(chat_id=update.effective_chat.id, text=quote, reply_to_message_id=update.effective_message.message_id)
setattr(self, 'on_'+name, fun)
def is_allowed(self, update, context): def is_allowed(self, update, context):
if self.all_channel_allowed: if self.all_channel_allowed:
return True return True
@ -82,7 +116,7 @@ class Bot:
"user": update.effective_user.name, "user": update.effective_user.name,
"message": getattr(update.effective_message, "text", ""), "message": getattr(update.effective_message, "text", ""),
} }
for i,quote in enumerate(self.pings): for i,quote in enumerate(self.commands.get(self.inline, [])):
actual_quote = "" actual_quote = ""
if not callable(quote): if not callable(quote):
actual_quote = quote.format(**c) actual_quote = quote.format(**c)
@ -99,28 +133,6 @@ class Bot:
context.bot.answer_inline_query(update.inline_query.id, results) context.bot.answer_inline_query(update.inline_query.id, results)
def on_ping(self, update, context):
if not self.is_allowed(update, context):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="Sorry, mamma told me not to talk with random strangers. You need to allow chat id {}.".format(update.effective_chat.id),
reply_to_message_id=update.effective_message.message_id
)
return
c = {
"channel": update.effective_chat.title,
"name": self.name,
"user": update.effective_user.name,
"message": update.effective_message.text,
}
quote = random.choice(self.pings)
if callable(quote):
quote = quote(self, update.effective_user, update.effective_chat, update.effective_message)
else:
quote = quote.format(**c)
context.bot.send_message(chat_id=update.effective_chat.id, text=quote, reply_to_message_id=update.effective_message.message_id)
def on_message(self, update, context): def on_message(self, update, context):
if not self.is_allowed(update, context): if not self.is_allowed(update, context):
return return
@ -149,14 +161,6 @@ class Bot:
""" """
self.add_reaction(match, self.fetch_callback(reaction)) self.add_reaction(match, self.fetch_callback(reaction))
def add_ping(self, reaction):
"""Add a reaction to a ping"""
self.pings.append(reaction)
def add_python_ping(self, reaction):
"""Fetch a Python callable and add it to the pings"""
self.add_ping(self.fetch_callback(reaction))
def fetch_callback(self, path): def fetch_callback(self, path):
"""Fetch a Python callable""" """Fetch a Python callable"""
s = path.split(".") s = path.split(".")
@ -175,7 +179,7 @@ class Bot:
Returns: Returns:
Every matched reactions. Every matched reactions.
""" """
if (datetime.datetime.now() - self.channels[channel.id]).total_seconds() < self.min_time: if channel.id in self.channels and (datetime.datetime.now() - self.channels[channel.id]).total_seconds() < self.min_time:
return [] return []
context = { context = {

50
klafytg/bots/klafyvel.py Normal file
View file

@ -0,0 +1,50 @@
import requests
from bs4 import BeautifulSoup
import random
URL_BASE = "https://crisco2.unicaen.fr/des/synonymes/"
def synonyms(word):
page = requests.get(URL_BASE+word)
soup = BeautifulSoup(page.text, features="html.parser")
syn = soup.find_all(id='synonymes')[0]
buff = syn.div
while buff != " Fin titre (vedette + nb de synonymes)":
buff = buff.next_sibling
while buff != "Fin liste des synonymes":
if buff.name and buff.name == "a":
yield buff.string
buff = buff.next_sibling
def make_corpus(start, max_depth=2, exclude=set()):
if max_depth <= 0:
return start
res = set(start)
for word in start:
syn = set(synonyms(word))
res = res.union(make_corpus((syn - res) - exclude, max_depth-1, res.union(syn)))
return res
adjectives_seed = {"idiot"}
nuns_seed = {"patriarcat", "capitaliste", "banquier"}
adjectives = list(make_corpus(adjectives_seed).union(adjectives_seed))
nuns = list(make_corpus(nuns_seed).union(nuns_seed))
def attack():
adj = str.capitalize(random.choice(adjectives))
nun = random.choice(nuns)
if nun[0] in 'aeiouéàè':
link = "d'"
else:
link = "de "
return ' '.join([adj, link+nun, '!'])
def on_attack(bot, update, context):
# you can choose to reply here or to simply return a str wich will be returned.
context.bot.send_message(
chat_id=update.effective_chat.id,
text=attack(),
reply_to_message_id=update.effective_message.message_id
)