LoginSignup
6
6

More than 3 years have passed since last update.

PythonとかJsonとか全く知識がない人がBot作った話

Last updated at Posted at 2020-12-14

前置き

https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4
この記事書いてくれたBotつよつよマンのおかげで意欲がわいた。

あと、マホ姫が喋ってほしかったから

あと、日本語クソ雑魚なので自分でも何言っているかわからないのでコード呼んで頑張って理解してください()
僕は雰囲気でプログラミングをやってます

作ったやつ

マホ姫botっていうやつ
sスクリーンショット 2020-12-14 183945.png
かわいいね
今回はマホ姫の機能の時間割機能について話したいと思います

時間割機能

スクリーンショット 2020-12-14 204843.png
マホ姫時間割教えてっていうと時間割を教えてくれます。
この機能の大まかな流れは「マホ姫今日の時間割は1,科目名2,科目名3,科目名4,科目目5,科目名」っていうと科目名のところをgooglespreadsheetに記入し「マホ姫時間割教えて」っていうとgooglespreadsheetからデータを取り出し出力する流れです。

1. googlespreadsheet apiの取得について

apiについて
アクセストークンを取得してそのソフトウェアの機能を利用することだとおもう。

googlespreadsheetapi の取得については以下のURLを参照してください。
https://qiita.com/164kondo/items/eec4d1d8fd7648217935 (Google Spread Sheets に Pythonを用いてアクセスしてみた)

2.科目名の取得

科目名をトゥートから抜き出しgooglespreadsheetに出力する記述を考える

Bot.py
        if re.search(r'((今日|きょう)の(時間割|じかんわり)は)', content):
            nagasa = len(content)
            firstclass = content[content.find('1,') + 2: content.find('2,')]
            secondclass = content[content.find('2,') + 2: content.find('3,')]
            thirdclass = content[content.find('3,') + 2: content.find('4,')]
            fourthclass = content[content.find('4,') + 2: content.find('5,')]
            fifthclass = content[content.find('5,') + 2: nagasa]

pythonのfind関数を使って科目名の場所を特定して1限目、2限目・・・・5限目のデータを変数に入れて科目名を取得する。

3,googlespreadsheetに記入

googlespreadshhetに1で取得した科目名を記入する

Bot.py
                    a = 3
                    while(str(sh.cell(a, 1).value) != ''):
                        if str(sh.cell(a,1).value) == username:
                            sh.update_cell(a,2,firstclass)
                            time.sleep(1)
                            sh.update_cell(a,3,secondclass)
                            time.sleep(1)
                            sh.update_cell(a,4,thirdclass)
                            time.sleep(1)
                            sh.update_cell(a,5,fourthclass)
                            time.sleep(1)
                            sh.update_cell(a,6,fifthclass)
                            time.sleep(1)
                            sh.update_cell(a,7,'ok')
                            time.sleep(1)
                            break
                        else:
                            a += 1
                    else:
                        sh.update_cell(a,1,username)
                        time.sleep(1)
                        sh.update_cell(a,2,firstclass)
                        time.sleep(1)
                        sh.update_cell(a,3,secondclass)
                        time.sleep(1)
                        sh.update_cell(a,4,thirdclass)
                        time.sleep(1)
                        sh.update_cell(a,5,fourthclass)
                        time.sleep(1)
                        sh.update_cell(a,6,fifthclass)
                        time.sleep(1)
                        sh.update_cell(a,7,'ok')
                        time.sleep(1)

トゥートのデータからユーザーのusernameを取得して(この記事ではusernameの取得方法については省略している)もしusernameがspreadsheetに記入されていればそのままセルに科目名を記入しなければusernameを記入したのちにセルに科目名を記入する。記入が成功すればこのようになるスクリーンショット 2020-12-14 222345.png

4,spreadsheetのデータをマストドンに出力する

spreadsheetで記入した科目のデータをマストドンに出力する

Bot.py
                a = 3
                while(str(sh.cell(a, 1).value) != ''):
                    if  str(sh.cell(a, 1).value) == username:
                        if(sh.cell(a,7).value == 'ok'):
                            mastodon.toot('時間割は\n1限目は'+sh.cell(a,2).value+'\n2限目は'+sh.cell(a,3).value+'\n3限目は'+sh.cell(a,4).value+'\n4限目は'+sh.cell(a,5).value+'\n5限目は'+sh.cell(a,6).value+'\nやわ~')
                            break
                        else:
                            mastodon.toot('時間割登録してないわ~登録してな?')
                            break
                    else:
                        a += 1
                else:
                    mastodon.toot('時間割分からへんわ~教えておくれやす~♪\n時間割の登録の方法は「マホ姫今日の時間割は1,科目名,2,科目名3,科目名4,科目名5,科目名」にして空きコマなら無しって書いておくれやす~')

これも3と同様にusernameが一致しているかどうかで識別してusernameがない場合は登録するように促してusernameが一致し時間割を登録していたらデータを取得してマストドンでトゥートする記述です。
スクリーンショット 2020-12-14 204843.png
やったねマホ姫が時間割をトゥートしたよ!!

最後に

全くpythonとかjson知らない人がBot作り始めてここまでできるようになったよっていう話です。はい。
もしBot作りたい!!っていう人がいたらC3の某Botつよつよマンの記事を見ればMastodonBotの基礎を作ることができます。
https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4

参考文献

【入門】PythonでMastodonBotを作りたい!【初心者向け】
https://qiita.com/PigeonsHouse/items/a6a6833e81c8dee384a4
Google Spread Sheets に Pythonを用いてアクセスしてみた
https://qiita.com/164kondo/items/eec4d1d8fd7648217935

6
6
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
6
6