LoginSignup
11
9

More than 3 years have passed since last update.

(Twitter)Pythonでリプライにいいねして返信するbotを作る(怪しい日本語ジェネレーターbot)

Last updated at Posted at 2019-07-26

とりあえず動かす

初心者が書いたものなのでなぜか認証が2つあったりスマートではないです。とりあえず動けばいいや!って方に...(寝ぼけてたまま書いてるので後で再チェックします..)

reply.py
# -*- coding: utf-8 -*-
import requests
import linecache
import tweepy
import json
#from requests_oauthlib import OAuth1Session
Twitter_ID = "自分のID(@はいりません)"
SCREEN_NAME = '自分のアカウントの名前'
CK="取得したやつ"
CS="取得したやつ"
AT="取得したやつ"
AS="取得したやつ"

#api = OAuth1Session(CK, CS, AT, AS) 後で見たら要らなかったです

auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)

api2 = tweepy.API(auth)
timeline=api2.mentions_timeline(count=1)

for status in timeline:
      status_id=status.id
      screen_name=status.author.screen_name.encode("UTF-8")
      scrn = screen_name.replace(' ', '')
      scr = scrn.rstrip('\n')
      print("@" + scr)
      print(status.text)
      inp = status.text
      inp = inp.lstrip("自分のID(@つける)")
      print(inp)
#ここから好きな処理
#この場合は怪しい日本語に変換してます
response = requests.get('https://correctjp.work/api/index.php?text='+inp+'&json=no&type=normal')
#ここまで好きな処理
#response.textに返信するテキストが入るようにすればOK
      reply_text="@"+screen_name+" "+response.text
      api2.create_favorite(status.id)
      if str(status.in_reply_to_screen_name)==Twitter_ID and str(status.user.screen_name)!=Twitter_ID:
          api2.update_status(status=reply_text,in_reply_to_status_id=status_id)
else:
    print("エラー")

これを保存して

crontab -e

を実行して

* * * * * python /置いてる場所/reply.py > /var/log/cron.log 2>&1
* * * * * sleep 10; PYTHONIOENCODING=utf-8 python /置いてる場所/reply.py > /var/log/cron.log 2>&1
* * * * * sleep 20; PYTHONIOENCODING=utf-8 python /置いてる場所/reply.py > /var/log/cron.log 2>&1
* * * * * sleep 30; PYTHONIOENCODING=utf-8 python /置いてる場所/reply.py > /var/log/cron.log 2>&1
* * * * * sleep 40; PYTHONIOENCODING=utf-8 python /置いてる場所/reply.py > /var/log/cron.log 2>&1
* * * * * sleep 50; PYTHONIOENCODING=utf-8 python /置いてる場所/reply.py > /var/log/cron.log 2>&1

一番下にこれを追記します。


sudo service cron restart

で動きます!

解説

ここから先は独り言みたいなものなので見てもメリットはありません。有限の人生の時間を無駄にしないためにブラウザバック推奨です。

CK,CS,AT,AS

  • CK=consumer key
  • CS=consumer secret
  • AT=access token
  • AS=access token secret

です。APIの申請、アプリの作成、APIキーの取得については他の人の記事を参照してください。

api = auth = tweepy..から..weepy.API(auth)

auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)
api2 = tweepy.API(auth)

ここでAPIと接続してTwitterAPIが使えるようにします。

timeline=api2.mentions_timeline(count=1)

最新のリプライを取得します。countを変えると前のリプライにも返信します。

for status in timeline:..から..print(inp)

正直自分でも分からないです。

status_id=status.id
screen_name=status.author.screen_name.encode("UTF-8")

status_idにstatus.idに突っ込んでるのと名前で日本語が使えるようにUTF-8にしてます。

 scrn = screen_name.replace(' ', '')
      scr = scrn.rstrip('\n')
      print("@" + scr)
      print(status.text)
      inp = status.text
      inp = inp.lstrip("自分のID(@つける)")
      print(inp)

文字列の処理です。
返信するユーザー名に@をつけたり改行を削除したりです。

inp = inp.lstrip("自分のID(@つける)")

ここに自分のIDを@付きで書きます。(返信時に自分のユーザー名を含めないため)

余計な処理は適当に削除してください...

response = requests.get('https://correctjp.work/api/index.php?text='+inp+'&json=no&type=normal')

response = requests.get('https://correctjp.work/api/index.php?text='+inp+'&json=no&type=normal')

ここには好きな処理を入れてください。response.textとしてリプしたいテキストが出力されればOKです。inpにはリプされた文字列が入ります。(@や空白などは先ほどの処理で削除済み)

reply_text="@"..から..tatus_id)

  reply_text="@"+screen_name+" "+response.text
      api2.create_favorite(status.id)
      if str(status.in_reply_to_screen_name)==Twitter_ID and str(status.user.screen_name)!=Twitter_ID:
          api2.update_status(status=reply_text,in_reply_to_status_id=status_id)
 reply_text="@"+screen_name+" "+response.text

リプライを用意します。

api2.create_favorite(status.id)

リプをファボります。

if str(status.in_reply_to_screen_name)==Twitter_ID and str(status.user.screen_name)!=Twitter_ID:

無限ループを起こさないように、返信先が自分の場合は返信しません。(自分が含まれてる場合に変更する必要があるかも、)

api2.update_status(status=reply_text,in_reply_to_status_id=status_id)

リプライを送信します。

else: print("エラー")

そのままです。できなかった時にエラーと表示します。

お疲れ様でした!

(適当なので鵜呑みにしないでください...)

11
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9