Code relating to my twitter bot, which can be found here.
For more information on interacting with the bot, see USERGUIDE.md
.
Fork it.
Get your Twitter api tokens.
Create a copy of configs/template.ini
as configs/config.ini
and store your credentials in it.
In the root directory of the project, create your conda env with conda env create -f twitterbot.yml
.
conda activate twitterbot
python client.py --config configs/config.ini
To create commands for your bot:
- Create a
Command
subclass using theCommand
Abstract Base Class. - Import the command into
client.py
- Add the command to the
commands_dict
inclient.py
along with a string alias for twitter users.
Your Command
subclass will recieve:
- The Twitter API
- The Tweet ID
- The User ID
- Any text following the command, for parameters.
Your subclass should implement a reply_tweet()
method that probably calls another method to do a thing, then prepares a response and tweets it back at the user.
For Example, you might make a class like this:
class MyCommand (Command):
def _do_something(self):
#do something
return 100
def _format_output(self, result):
# string format it or whatever
return str(result)
def reply_tweet(self):
something = self._do_something()
text = self._format_output(something)
self.api.PostUpdate(
status=text,
in_reply_to_status_id=self.tweet_id,
auto_populate_reply_metadata=True
)
Then you can add it to client.py
's commands_dict
like this:
from my_commands import MyCommand
commands_dict = {
'my_command': MyCommand,
}
And you should be good to go!
Tweet at your bot using the following format: "@bot command (params)"