Bot is the etymology of Robot and refers to something that does something automatically. On twitter
--Automatic Tweet Bot --Automatic Like Bot --Automatic follow bot
There are a wide variety of bots.
Automatic tweets act like notifications. For example, "Tweet automatically when the live information of your favorite artist is updated". Auto-like and auto-follow work as a tool to let an unspecified number of people know your account. This is used, for example, for PR activities on your Twitter account.
The procedure for creating a bot is mainly the following 3 steps.
This time, we will create a bot that regularly tweets the current time. Details are introduced in the following video.
https://youtu.be/Ab6TU9sFBM4
English edition https://youtu.be/mmKXdLUhG_k
credential.py
CONSUMER_KEY = '********'
CONSUMER_SECRET = '********'
ACCESS_TOKEN_KEY = '********'
ACCESS_TOKEN_SECRET = '********'
twitter_bot.py
from credential import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET
from requests_oauthlib import OAuth1Session
from http import HTTPStatus
from datetime import datetime
def post_tweet(body):
#Authentication process
twitter = OAuth1Session(
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET
)
#Tweet processing
res = twitter.post("https://api.twitter.com/1.1/statuses/update.json", params={"status": body})
print(res)
#Error handling
if res.status_code == HTTPStatus.OK:
print("Successfuly posted")
else:
print(f"Failed: {res.status_code}")
def main():
# body = "Test post 2"
now = datetime.now()
post_tweet(now)
if __name__ == '__main__':
main()
Recommended Posts