With Mastodon, a normal toot, a bot that makes a specific reply when a specific word is included when it is replied, a cloud, or a person that automatically runs on the network not only on the own machine but also on the server! I struggled to make. If you want to make something like that, it may be enough to read this article. If you want to see the transition that I did my best from the beginning, please click ↓. Memo when creating Mastodon Bot: Part 1 Normal Toot (local) --Qiita
__ Maybe it's impossible to maintain without spending money! __ If you have mackerel at home, you may be able to do it ... For those who refer to such a weak page I feel like you're thinking of making it with a light feeling, so Please be prepared for the cost anyway. Is the price of Sakura VPS about 1500 yen per month? It may be good to do various things with VPS, but anyway, I am self-satisfied.
I will summarize it easily (probably).
Upload files (use vi minimally ...) and install libraries Prepare a server for crontab (the one for scheduling and tweeting). I used Sakura VPS. My local! The main environment of is Windows 10. I don't know if it can be done at the command prompt, but I use Tera Term. Install Python, pip, Mastodon.py ... If you don't know, CentOS! (What I want to find out)! Install! In [Search] Maybe something will come out. If you don't understand, ask me if you can understand it.
CUI ... Isn't it difficult for those who haven't experienced the feeling of just typing English on that black screen? How to use TeraTerm [Search] Please do it. And commands Notes on frequently used commands in Tera Term | Online Inc.
I will also refer to the next item, Anyway, please see the article below. It's not a VPS, but refer to Mastodon.py's Insco! Is your order a Mastodon Bot? For first-time users who want to move it now ☕ --Qiita
Is your order a Mastodon Bot? For first-time users who want to move it now ☕ --Qiita This completely uses the code of the article (?) In ↑. Copy the code from setup.py, save it, This is on the server (although it can be done locally)
python setup.py
Then, two files, client.secret and user.secret, will be created. Here are the files you need to toot.
I don't know why, but I got an error when the instance was mstdn.jp. I could have done it with myself and this instance! why! ??
Please try it for the time being with bot.py described in the above article. Chino-chan! I'm supposed to toot ... I like Cocoa too!
So for the time being, create data.txt to mutter the appropriate toot content. The content is really appropriate. I will prepare it for those who can not decide!
data.txt
Normal toot 01
Normal toot 02
Normal toot 03
Normal toot 04
Normal toot 05
Normal toot 06
Normal toot 07
Normal toot 08
Normal toot 09
Normal toot 10
\ Doya! / For the time being, it's a trial to mutter randomly, so it's okay. One line is one toot. If you want to make a difference, think for yourself! So, the contents of bot.py for randomly muttering are as follows.
bot.py
# -- coding: utf-8 --
from mastodon import Mastodon
import random
hogebot = Mastodon(
client_id = 'client.secret', #app information
access_token = 'user.secret', #Login token
api_base_url = 'https://hogehoge.com' #Instance name
)
#Specifying a normal Toot file
path = 'data.txt'
#Read the file and mutter
with open(path, 'r') as f:
l = f.readlines()
content = random.choice(l)
# Toot!!
hogebot.toot(content)
print('[Posting completed]' + str(content)) #For confirmation
At the top, I use UTF-8, so it's magic. Rewrite only the instance name, Put data.txt, client.secret and user.secret in the same place,
python bot.py
Should work. By the way, the last print statement is the type I want to see the result, so I just added it, so it's okay to delete it. (In fact, it seems that mail will come one by one when it is finally decided to run with cron It may be better to delete it) (There seems to be a way to stop mail from coming ...) And a variable called hogebot ... variable? I'm sorry for my name Please give me a cute name. There are two places.
Write the contents of reply_pattern.txt first.
reply.text
Hello::Hello01
Hello::Hello02
Hello::Hello03
Hello::Hello04
Hello::Hello05
Good evening::Good evening01
Good evening::Good evening02
Good evening::Good evening03
Good evening::Good evening04
Good evening::Good evening05
It's appropriate again ... Say "Hello" to reply to the "Hello n", When you say "Good evening", the content is to reply "Good evening n". This is also one group in one line, The front of :: is the pattern of words and phrases that the other party said, and the back is the reply content on the bot side. Oh, I want to be able to express reaction words normally ... Let's think about it later ...
reply.py
# -- coding: utf-8 --
from mastodon import Mastodon, StreamListener
import requests
import random
import re
import os.path
def main(content,st,id,disname):
#Specifying a mutter file
path = 'reply_pattern.txt'
#Read the file and mutter
with open(path, 'r') as f:
l = f.readlines()
count = len(l) - 1;
selectedList = []
hogeToot = ''
nameChanged = ''
#Extract lines containing reaction words
while count >= 0:
tmpList = l[count].split('::')
if tmpList[0] in st['content']:
selectedList.append(tmpList)
count -= 1
#Randomly select one from the extracted list
if len(selectedList) != 0:
count = len(selectedList) - 1;
replyNo = random.randint(0,count)
hogeToot = selectedList[replyNo][1]
if hogeToot == '':
hogeToot = '{name}I'm sorry, I couldn't hear you'
# {name}To your name
nameChanged = re.sub("\{name\}",disname,hogeToot)
#↓ Processing when there is no toot, change soon
print("【reply】" + nameChanged) #For confirmation
mastodon.status_reply(st,
nameChanged,
id,
visibility='public') #Disclosure range
#"Release"-> 'public'
#"Unlisted"-> 'unlisted'
#"private"-> 'private'
#"direct"-> 'direct'
mastodon = Mastodon(
client_id = "client.secret",
access_token = "user.secret",
api_base_url = "https://hogehoge.com") #instance
notif = mastodon.notifications() #Get notifications
count = len(notif) - 1
while True:
#Reverse(Older one)Count Toot from (0)/Break when you reach the end)
if count >= 0:
if notif[count]['type'] == 'mention':
#Toot formation...I think that the
content = notif[count]['status']['content']
id = notif[count]['status']['account']['username']
st = notif[count]['status']
disname = notif[count]['status']['account']['display_name']
idStr = str(notif[count]['status']['id'])
# Toot!!/log.When txt exists(The second and subsequent executions)Only mutter
if os.path.exists('log.txt'):
main(content, st, id, disname)
else:
#Clear notifications when you have picked up notifications to the end
mastodon.notifications_clear()
if not os.path.exists('log.txt'):
f = open('log.txt', 'w')
f.write('1')
f.close()
break
count -= 1
I forgot to write in the articles up to Part 3, Towards the end, I added a process to say that if the dirt has already received notifications ... If so, if you're not good at it, you could explode hundreds of rips ... When you run it for the first time, clear all notifications and create a file called log.txt. (So I will not reply to the notifications I received by that time) When there is log.txt (at the time of the second and subsequent executions), the process of replying normally is performed.
This file also works just by changing the instance name ... should!
Is crontab a function that allows you to execute your favorite files and commands at your favorite time? is not it. You can run it for a minimum of 1 minute, and you can run it every week on what day of the week, at what hour and at what minute, and so on. Check crontab for details!
crontab -e
You can go to the edit screen with. Is it the same as vi ...? ?? ?? ??
0 * * * * cd /root/mstdn && python ./bot.py
* * * * * cd /root/mstdn && python ./reply.py
I was very addicted to it, but it seemed that I couldn't execute it until I moved it with cd. This should work! Thank you for your support! !!
Recommended Posts