Good day, dear Habrasoobschestvo.
Today is Friday, which means that you can escape from serious projects and relax. For example, read the next article for beginners, devoted to the development of a simple twitter bot in python, notifying of several types of GitHub activity.
If you suspect that you will not learn anything new from this article, you can simply see
the project code on GitHub . I invite the rest under the cat to learn more about libraries for interacting with GitHub API v3 and get acquainted with the process of writing a bot.
')

I note that on Habrahabr it is easy to find tutorials on writing bots for Twitter (
[1] ,
[2] ,
[3] , ...) and difficult - by the API for GitHub. This is perhaps the main reason for the appearance of this note. In addition, before you is just a simple example of using the huge opportunities that are available to us.
Github API
Currently, the current version of the API is the third (support for the first two
was canceled in June of this year). To work with it through python, there
are at least six libraries that are very similar. I chose the library based on the availability of user-friendly documentation and the absence of a large number of dependencies. The most pleasant seemed
github3.py .
Twitter API
From Twitter'a required very little - it was necessary to be able to log in, receive new mentions (mentions) and send messages. The token could be obtained manually (it is issued once and does not need to be updated). So I stopped at the
oauth library, ported to the third python.
Bot development
It was assumed that the bot will work as follows:
- every N seconds to update the messages in which it was mentioned
- find new ones among them
- form command messages
- execute commands
- report performance results to users
The structure of the project is as follows:

The oauth folder contains a library for authorization, in the twitter.py file methods for sending messages and updating the tape, github.py, in fact, is the bot itself, and id.dat will store the number of the last processed message (in case of any failures in the work). Config.py is needed for storing keys and passwords.
To work with the Twitter API, you need to create your application. Log in to dev.twitter.com, click “Create an app”, fill out the form and create it with the application. To send messages, you must enable the "Read and write" mode in the "Settings" section. It remains to get the keys (“Create my access token” on the “Details” tab) and you can proceed.
twitter.py
Links to the full code will be at the end of the article, here are just brief excerpts.
First, we will teach our bot to log in to Twitter, send requests and save the number of the last message.
HOME_TIMELINE_URL = 'http://api.twitter.com/1/statuses/home_timeline.json' UPDATE_URL = 'http://api.twitter.com/1/statuses/update.json' MENTIONS_URL = 'http://api.twitter.com/1/statuses/mentions.json' FILENAME = 'id.dat'
To update the tape is enough to establish a connection:
def _get_connection(self): self.connection = http.client.HTTPConnection('twitter.com')
And send a POST request:
oauth_request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, token=self.access_token, http_method='POST', http_url=UPDATE_URL, parameters=params) oauth_request.sign_request(self.signature_method, self.consumer, self.access_token) self.connection.request(oauth_request.http_method, UPDATE_URL, headers=oauth_request.to_header(), body=self._to_query_string(params))
And to get the mention - GET request:
oauth_request =\ oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.access_token, http_method='GET', http_url=MENTIONS_URL, parameters=params) oauth_request.sign_request(self.signature_method, self.consumer, self.access_token) self.connection.request(oauth_request.http_method, MENTIONS_URL + '?' + self._to_query_string(params), headers=oauth_request.to_header())
To save information, you can use the pickle library:
def _save(self, data): path = os.path.dirname(__file__) path = os.path.join(path, FILENAME) log_file = open(path, 'wb') pickle.dump(data, log_file) log_file.close()
In principle, this will be enough for our task. Now we will create a config.py file in which we will store the keys we received for the Twitter API and the login / password of the GitHub user (in case our bot needs it):
import twitter secret = twitter.SecretKeys('consumer_key', 'consumer_secret', 'auth_key', 'auth_secret') LOGIN = 'github_login' PASSWORD = 'github_password'
And we will add a shell for their storage:
class SecretKeys: keys = {} def __init__(self, consumer_key, consumer_secret, auth_key, auth_secret): self.keys = {'consumer_key': consumer_key, 'consumer_secret': consumer_secret, 'auth_key': auth_key, 'auth_secret': auth_secret}
github.py
From the bot itself takes a little more. It will download a list of new messages, form a list of tasks from them, perform tasks and notify users of their results.
First we need the following libraries:
from github3 import login import twitter
The last message number will be taken from the file:
def get_data(filename): with open(filename, 'rb') as file: return pickle.load(file)
The list of new tasks we get from twitter.py:
list_of_problems = api.get_new_mentions(idx)
And we will go through it, forming new teams:
for problem in list_of_problems: problem = form_problem(problem)
The form_problem () function will search for what exactly the user wanted from the bot and form a dictionary with parameters:
if command in commands: command = commands.index(command) user = problem['user']['screen_name'] return {'command': command, 'user': user, 'params': params}
Then you will need to complete each of the tasks:
def solve(problem, gh): functions = {0: get_last_commit, 1: get_list_of_contributors, 2: get_count_of_open_issues, 3: get_count_of_commits, 4: get_count_of_repos, 5: subscribe_on_commits, 6: help, 7: unsubscribe_from_commits} return functions[problem['command']](gh, problem['params'], problem['user'])
Send results to user:
api.post_update('@%s %s.' % (problem['user'], result))
And make a short pause:
time.sleep(10)
In principle, this is enough for the first version. The functions themselves are completely based on the GitHub API and github3.py documentation. For example, the output of the last commit in any repository can be implemented as follows:
def get_last_commit(gh, params, user): pattern = 'Last commit in "%s" was "%s" by %s' repository = gh.repository(params[0], params[1]) last_commit = repository.list_commits()[0] return pattern % (repository.name, last_commit.commit.message, last_commit.commit.author.name)
From the point of view of the
GitHub API , this is a GET request (/ repos /: user /: repo / commits), in response to which the server will return a list of all commits in the repository with quite extensive information:

Perhaps that's all. To start the bot, you just need to run the github.py file, first filling out config.py. Currently, GitToTweet supports the following commands:
- Print the last commit in the repository:
get last commit, ,
: get list of contributors, ,
: get count of open issues, ,
: get count of commits, ,
: get count of repos, ,
: subscribe me, ,
: unsubscribe me, ,
github: help
:
get last commit, ,
: get list of contributors, ,
: get count of open issues, ,
: get count of commits, ,
: get count of repos, ,
: subscribe me, ,
: unsubscribe me, ,
github: help
:
@GitToTweet get last commit, django, django
@% username% of the last commit in "django" was "Fixed # 18847 - Thanks for Jamie Curle ...
Links
Project source code on GitHub:
github.com/valzevul/GitToTweet
GitHub API v3 documentation:
developer.github.com
Twitter API documentation:
dev.twitter.com/docs
An example of how a bot works on Twitter can be found here:
twitter.com/GitToTweet
Thanks for attention.