I'm a non-programmer, but I made a Twitter Bot because I wanted to make something myself. We hope that it will be helpful for beginners when creating bots.
It is a bot that randomly acquires two words from the words registered in wikipedia and tweets them. https://twitter.com/Sosaku_Tango
First, here is the completed code.
import tweepy, random
import schedule
import time
#Substitute each acquired key
CK=""
CS=""
AT=""
AS=""
#Instance creation
auth = tweepy.OAuthHandler(CK, CS)
auth.set_access_token(AT, AS)
api = tweepy.API(auth)
#Tweet two words from the downloaded wikipedia word list file
def bot():
wiki_titles = open("jawiki-latest-all-titles-in-ns0.txt", "r")
line = wiki_titles.readlines()
random_word = random.sample(line, 1)
first_word = ''.join(random_word)
random_word = random.sample(line, 1)
second_word = ''.join(random_word)
api.update_status('1.' + first_word + '2.' + second_word + '\Can you create with n? I'm looking forward to the completion. #Creative word')
#Regular execution
def main():
schedule.every().day.at("10:00").do(bot)
while True:
schedule.run_pending()
time.sleep(1)
main()
For the basic method of creating TwitterBot using Tweepy, refer to here. https://qiita.com/tsc343/items/e51f412480ea8bf5619a
Wikipedia word data (.txt) is downloaded from here. https://dumps.wikimedia.org/jawiki/
Refer to here for the code that randomly extracts lines from the txt file. http://tksmd.hatenablog.com/entry/20090122/p1
Refer to here for regular automatic execution. https://qiita.com/Kai-Suzuki/items/0c5c0e5cbdb4075fe482
Completion. Thank you for your hard work.
Until recently, I used to run it manually every day, but I often forgot it, so I put in the code for autorun. I took this opportunity to post for the first time on Qiita. Since there are no followers or likes, no one recognizes it, but if you are interested, I would be happy if you could see it once. If you notice anything, I would appreciate it if you could comment.
Recommended Posts