Good day, dear Habrayuzer.
This post is inspired by the appearance of an
article about the Twitter bot and all sorts of dancers for him. I present to you my vision of api Twitter. Welcome under cat.
It should be noted that this topic was published by me in the sandbox, when I was still unregistered. And then, unexpectedly, another invite came to me, so I’m posting the topic as it is, without any changes, and I’m going to inform the caliper about the second account.
So, as the title states, the bot application will use the api tweeter to control the machine with the linux onboard.
')
Python was chosen as the development language and, as a result, the tweepy library. Hout on the initial setup of this library can be found on the link above.
Configuration
We proceed directly to the subject. The code is designed as a full-fledged Linux-daemon, has a configuration file and works with plug-ins.
Consider the configuration file:
Everything is transparent. To get started, you only need to fill in the consumer_key, consumer_secret, user keys - access_key and access_secret, previously received. You also need to add nicknames of people who are allowed to manage the system in AUTHORIZED_ACCOUNTS.
Plugins
Let's go to the plugins. All functionality is provided only with their help. The plugin template looks like this:
class Plugin(object): name = 'undefined' keyword = 'undefined'
where name is the name of the plugin, keyword is its unique identifier by which it is accessed.
Action is a necessary action, that is, the plugin can be silent, respond to you in a personal, or write in your timeline.
The controlled field tells the plugin whether it will be interactive — accept and process client messages. If the plug-in is interactive, then it listens to messages that come to the bot and, if the message is addressed to it, calls the execute method, in which you can implement anything. If the plugin is “silent”, it runs the code from the run () method in a separate thread.
How to use it
Just send the bot a private message of the form “keyword command args”. The bot parses the message and passes it to the processing of the plugin with the corresponding keyword. For example, consider the plugin uptime. It receives only one parameter, the keyword, and reports the uptime of the machine on which it is running to its timeline.
Implemented plugins
conntest - constant check of the link with the remote host (s). He writes about problems in the timeline.
removetweets - removes n tweets from the bot's timeline (example “removetweets 5”).
sshmonitor - monitoring ssh server based on auth.log parsing. Writes an authentication error in the timeline.
uptime - displays machine uptime.
That's all, you can implement any plugin to your taste and monitor anything (or restart services for example) via Twitter.
Thank you for your attention, waiting for your comments.
PS
Code on github.
PPS To start the daemon, you must use the link in the bin directory.