Create a Twitter BOT with the GoogleAppEngine SDK for Python

We create and operate Twitter BOT on the platform of Google App Engine (hereinafter referred to as GAE) that can be used for free for studying Python. BOT uses Google Alerts to regularly mutter love live news. Account: @ll_news_bot Source: GitHub

I will keep a memorandum of the procedure to operate BOT with GAE SDK for Python.

BOT specifications

The final specifications of the BOT created this time are as follows.

Advance preparation

Google alerts

Create new news from Google Alerts. At this time, set the delivery destination to ** feed **.

environment

I am using the following environment for local development.

Twitter

GoogleAppEngine Create a new application for BOT from appengine.google.com. The * Application Identifier * entered at this time will be used as the * Application ID * when creating a new application with GAEL launcher.

External module

We used the following third-party modules for BOT development. There are two types of modules, a Python wrapper for the Twitter API and an RSS feed parser.

The above module must be deployed on GAE along with the web application, so After installing or building the module, I had to copy it into my project.

BOT implementation

The functions of the created BOT are largely "Create a mutter from an RSS feed" and "Operation of Twitter API", so we will create a module for each of these processes.

feed module

This module uses the * feedparser * module to parse RSS feeds and create muttering content. We regularly check for new entries, so we use * DataStore * to avoid duplicate muttering.

class FeedFetcher(object):
    '''Class for parsing RSS feeds for Google Alerts'''

  	def fetch_new_entries(self):
    '''Get new entries from the Google Alerts RSS feed.'''
	#abridgement
	return new_entory

class Entry(object):
    '''Class for storing parsed entry information'''
    
 	def make_tweets(self):
	'''Create muttering content'''
	return #Muttering

#How to use
fetcher = FeedFetcher()
new_entries = fetcher. fetch_new_entries()
for entry in new_entries:
	print entry.make_tweet()

bot module

In this module, I wrap the python-twitter module so that I can easily hit the required API. In addition, the Twitter authentication information obtained at the time of preparation is passed to the python-twitter API. This is reference, and when using python-twitter with GAE, it is not cached. ..

class TwitterBot(object):
    '''Classes for muttering to Twitter, refollowing and unfollowing users'''

	def tweet(self, message):
	'''Mumble the target content to Twitter'''
	#abridgement

	def get_not_follow_users(self):
	'''Get the ID list of unfollowed users'''
	return #Unfollowed user ID list

	def refollow(self, not_follow_id):
	'''Follow the target user'''
	#abridgement

	# ...abridgement

#How to use
bot = TwitterBot()
fetcher = FeedFetcher()
bot.tweet(fetcher. fetch_new_entries[0].make_tweet)
bot.refollow(bot. get_not_follow_users()[0])

Request handler

First, create a * app.yaml * file for URL mapping. If login is admin, even if you hit the URL on the browser, the request will not be sent unless you authenticate your account. In script, write the * WSGIApplication * object name that maps the module name and URL.

application: ll-news-bot
version: 8
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  script: tweet.app
  login: admin

- url: /tweet
  script: tweet.app
  

On the python side, create a class that inherits * RequestHandler * and write the handle processing of the get or post method. I don't call the Twitter API in large numbers at once, but I use the task queue for studying. When the task is executed, a POST method request is sent to the specified URL.

tweet.py


class MainHandler(webapp2.RequestHandler):    
    def get(self):
		 taskqueue.add(queue_name='tweet', url='/tweet', params={'message': u'test'})

class TweetHandler(webapp2.RequestHandler):    
    def post(self):
		#Tweet processing
		bot = TwitterBot()
		bot.tweet(self.request.get('message')

app = webapp2.WSGIApplication([('/', MainHandler), ('/tweet', TweetHandler)], true) 

To use the task queue, create * queue.yaml *. The following site was very helpful for the explanation about the task queue.

app.yaml


queue:
- name: tweet
  rate: 1/s
  bucket_size: 1 
  retry_parameters:
    task_retry_limit: 0

Finally, create a * cron.yaml * file to run this process on a regular basis. Use * schedule * to set how often to execute.

cron.yaml


cron:
- description: tweet task
  url: /
  schedule: every 30 minutes from 9:00 to 23:59
  timezone: Asia/Tokyo

Local execution

Use the GAELaucher app and check the operation of the app in your local environment. You can create it from the Launcher app. At that time, use the * Application Identifier * of the application created in advance for the Application ID.

You need to run * Make Symlink * from the menu before running. Also, if the port number used by GAE is used by another app, you need to change either port number.

Deploy

Finally, use GAE Launcher to deploy the application to Google's servers. At that time, you will be asked to enter an account, so enter the Google account that corresponds to the application created in advance.

I sometimes see * Checking if deployment succeeded. * Endlessly during deployment, which seems to be a server-side issue. Finally, you will be asked to upgrade the version and redeploy or roll back, so this time I upgraded the version and redeployed and it worked.    The above is the flow from the development of Twitter BOT to its operation.

Recommended Posts

Create a Twitter BOT with the GoogleAppEngine SDK for Python
Create a LINE BOT with Minette for Python
Steps to create a Twitter bot with python
Let's make a Twitter Bot with Python!
Create a color picker for the color wheel with Python + Qt (PySide)
Make a Twitter trend bot with heroku + Python
Create a directory with python
I made a Twitter BOT with GAE (python) (with a reference)
Create a Layer for AWS Lambda Python with Docker
A note about hitting the Facebook API with the Python SDK
Create a list in Python with all followers on twitter
Create a Mastodon bot with a function to automatically reply with Python
Probably the easiest way to create a pdf with Python3
Try a similar search for Image Search using the Python SDK [Search]
Create a real-time auto-reply bot using the Twitter Streaming API
[Python] Create a screen for HTTP status code 403/404/500 with Django
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Create a Python execution environment for Windows with VScode + Remote WSL
Create a striped illusion with gamma correction for Python3 and openCV3
The story of making a standard driver for db with python.
[LINE Messaging API] Create a BOT that connects with someone with Python
Create a USB boot Ubuntu with a Python environment for data analysis
Create a compatibility judgment program with the random module of python.
Tweet the weather forecast with a bot
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Search the maze with the python A * algorithm
Working with OpenStack using the Python SDK
[Python] [LINE Bot] Create a parrot return LINE Bot
Create a word frequency counter with Python 3.4
Create a bot that boosts Twitter trends
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 1)
The story of making a university 100 yen breakfast LINE bot with Python
Create a Python development environment locally at the fastest speed (for beginners)
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Create a record with attachments in KINTONE using the Python requests module
Create a frame with transparent background with tkinter [Python]
[Python] Get the files in a folder with Python
Tweet the weather forecast with a bot Part 2
Created a Python wrapper for the Qiita API
Get a ticket for a theme park with python
Upgrade the Azure Machine Learning SDK for Python
I made a Twitter fujoshi blocker with Python ①
Use logger with Python for the time being
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Create a dashboard for Network devices with Django!
Python: Prepare a serializer for the class instance:
Create a color bar with Python + Qt (PySide)
Commands for creating a python3 environment with virtualenv
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)