Skip to content

Instantly share code, notes, and snippets.

@take01x
Created July 8, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save take01x/119ec8acf2101bc19b78 to your computer and use it in GitHub Desktop.
Save take01x/119ec8acf2101bc19b78 to your computer and use it in GitHub Desktop.
にゃんぱすーと挨拶されたらにゃんぱすーと返す簡単な Bot プログラムです
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import tweepy
import simplejson
import random
# infomation to access twitter api.
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
# authenticate the twitter account by OAuth.
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth)
# get bot name
bot_name = api.me().screen_name
# create streamlistener class to modify action in userstreaming.
class myStreamListener(tweepy.StreamListener):
# define the action when data come from twitter.
def on_data(self, data):
# the style of data which come from twitter is json, so start with "{".
if data.startswith("{"):
# convert json to Python dictionary, because it is very useful.
data = simplejson.loads(data)
# define the action when catch any tweet.
if "text" in data:
# if the tweet is reply to bot and it contains keyword, return a greeting.
if data['in_reply_to_screen_name'] == bot_name and "にゃんぱすー" in data['text']:
Tweet = "@" + data['user']['screen_name'] + " にゃんぱすー" + " " * random.randint(0,10)
api.update_status(status = Tweet, in_reply_to_status_id = data['id'])
# set information to start streaming
stream = tweepy.Stream(auth=api.auth, listener=myStreamListener(), secure=True, timeout=None)
# start userstreaming
stream.userstream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment