twitter botでmentionにreplyするほか

今考えているものが、TwitterBotとしてリリースするのが良いだろうということで、本体の前にヤックシェービング的にTwitterBotのほうを実装した。
というか、自分へのmentionに応答するBotが見つからなかったので作らざるおえなかった。

https://github.com/MichinariNukazawa/twitter_bot_mock




参考については以下の通り。



https://twitter.com/DaisyTestBot01
tweepyのサンプルが最初に動いたので採用した。
アプリ認証などは手動でした。

retweetやfavoをするのに使うメソッドは、apiのメソッド一覧を見ると載ってる。
http://docs.tweepy.org/en/v3.5.0/api.html


# mentionへのreply

全文はgithub参照。
statusオブジェクトに規定の要素が入っているので、それでmentionを判定する。
replyは、APIを使ってreply先としてmention元を指定したツイートを投げる。

```
         # ** retweet and reply when mention ("@" tweet)
        for user_mention in status.entities['user_mentions']:
            #print("receive mention: {0} {1}".format(status.user.screen_name, str(datetime.datetime.today())))
            #pp.pprint(user_mention)

            if str(status.user.screen_name) == client_info["screen_name"]:
                # exclude self mention.
                pass
            else:
                #print("receive mention: " + str(datetime.datetime.today()))
                # ** reply
                try:
                    tweet = "@{0} Hello!\n {1}".format(status.user.screen_name, str(datetime.datetime.today()))
                    api.update_status(status=tweet, in_reply_to_status_id=status.id)
                except TweepError as err:
                    pp.pprint(err)
                    #return False

```

# その他


途中で出たエラー。
```
    raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 226, 'message': "This request looks like it might be automated. To protect our users from spam and other malicious activity, we can't complete this action right now. Please try again later."}]
```


```
MN@daisy-bell:twitter_bot/$ python3 reply_bot.py
receive mention: 2018-04-21 22:38:07.842412
receive mention: 2018-04-21 22:38:08.467460
TweepError([{'code': 327, 'message': 'You have already retweeted this Tweet.'}],)
receive mention: 2018-04-21 22:38:08.962691
receive mention: 2018-04-21 22:38:09.624403
receive mention: 2018-04-21 22:38:11.172642
TweepError([{'code': 327, 'message': 'You have already retweeted this Tweet.'}],)

~
receive mention: 2018-04-21 22:38:14.510356
receive mention: 2018-04-21 22:38:15.128921
receive mention: 2018-04-21 22:38:15.829066
TweepError([{'code': 327, 'message': 'You have already retweeted this Tweet.'}],)
receive mention: 2018-04-21 22:38:16.467772
receive mention: 2018-04-21 22:38:17.081724
TweepError([{'code': 327, 'message': 'You have already retweeted this Tweet.'}],)
receive mention: 2018-04-21 22:38:17.602246
TweepError([{'code': 226, 'message': "This request looks like it might be automated. To protect our users from spam and other malicious activity, we can't complete this action right now. Please try again later."}],)

~
receive mention: 2018-04-21 22:38:19.886857
TweepError([{'code': 326, 'message': 'To protect our users from spam and other malicious activity, this account is temporarily locked. Please log in to https://twitter.com to unlock your account.'}],)

```

エラーそのものは単なる正常なTwitterのBot対策のアカウントロック。
Botを作っているのだから当然、開発中はコーディングミスで引っかかる。同じ内容のツイートを連続でしてしまったり。
今回の場合は、自分からのreplyに含まれている自分への”@付きツイート”をリツイートして無限ループに陥り、連続ツイートあたりでロックされた。

ロックされたら手作業で解除する。解除は簡単にできる。

で、tweepyはエラーを例外で投げ上げる。ネットワークアクセスの結果の失敗を例外で上げるのは個人的にはどうかと思うのだけれど、tweepyは便利なので、単にラップすることにした。
```
                 # ** retweet
                try:
                    api.retweet(status.id)
                except TweepError as err:
                    pp.pprint(err)

```

こういう場合もある。
```
receive mention: 2018-04-21 22:42:40.612219
receive mention: 2018-04-21 22:42:40.946000
TweepError([{'code': 226, 'message': "This request looks like it might be automated. To protect our users from spam and other malicious activity, we can't complete this action right now. Please try again later."}],)
```

0 件のコメント:

コメントを投稿

Linuxコマンドライン上でSVGベクタ画像をJPG等へラスタライズ変換する

 Linuxコマンドライン上でSVGベクタ画像をJPG等へラスタライズ変換することができるが、上手く変換されない場合がある。   以前作った魔法陣イラスト素材をイラスト素材ストックサイトへ登録しようと思い立ち、改めて素材用にラスタライズ変換をかけようとした。   ラスタライズ変換...