質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

3回答

13147閲覧

エラー an integer is required (got type str)

退会済みユーザー

退会済みユーザー

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/01/14 11:27

編集2021/11/15 02:12

こんにちは
LINEPYでBOTを作ろうとしているものです

本題に入りますが、
OEPollを使用して送られたメッセージを
"line.log"というファイルに保存するというスクリプトなのですが、

Error

1an integer is required (got type str)

が出てしまい、
"line.log"に内容を保存できませんでした

デバッグしてもどこが原因でファイルが保存できていないのかわからないです

Pythonの詳しい方、

解決法をお願いします

以下がソースコードです

python

1# -*- coding: utf-8 -*- 2 3from linepy import * 4import time, re, ast, sys, os, datetime 5 6try: 7 args = sys.argv 8 if len(args)>=2 and args[1] is not None: 9 cl = LINE(args[1]) 10 else: 11 print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-") 12 print("||| Read Bot by **** |||") 13 print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=") 14 print("") 15 print("___Login Mode List______") 16 print("| 1, URL Auth (Default)|") 17 print("| 2, authToken |") 18 print("| 3, Email and Password|") 19 print("________________________") 20 print("") 21 startCmdInt = input ("Please Choose Login Mode [1]: ") 22 print("") 23 if startCmdInt == "" or startCmdInt == "1": 24 cl = LINE() 25 elif startCmdInt == "2": 26 authToken = input("authToken: ") 27 print("") 28 cl = LINE(authToken) 29 elif startCmdInt == "3": 30 email = input("Email: ") 31 password = input("Password: ") 32 print("") 33 cl = LINE(email, password) 34 else: 35 cl = LINE() 36except Exception as e: 37 print("") 38 print(e) 39 print("") 40 input("Press Enter to Finish...") 41 exit() 42 43channelToken = cl.getChannelResult() 44cl.log("authToken: "+str(cl.authToken)) 45cl.log("channelToken: "+str(channelToken)) 46 47oepoll = OEPoll(cl) 48clMID = cl.profile.mid 49 50def restartBOT(): 51 time.sleep(3) 52 os.execl(sys.executable, sys.executable, [args[0], cl.authToken]) 53 54def clBot(op): 55 try: 56 57 if os.path.isfile("line.log"): 58 with open("line.log","r", "utf-8", "ignore") as f: 59 log = f.read() 60 else: 61 log = "" 62 if op.type == 0: 63 cl.log("[ 0 ] END OF OPERATION") 64 if op.type == 5: 65 cl.log("[ 5 ] NOTIFIED ADD CONTACT") 66 displayName = cl.getContact(op.param1).displayName 67 mid = cl.getContact(op.param1).mid 68 print("Add Contact: "+mid+"/"+displayName) 69 log = "["+str(datetime.datetime.now())+"] Add Contact: "+mid+"/"+displayName+"\n\n"+log 70 if op.type == 13: 71 cl.log("[ 13 ] NOTIFIED INVITE GROUP") 72 gid = op.param1 73 gname = cl.getGroup(gid).name 74 print("Invite Group: "+gid+"/"+gname) 75 log = "["+str(datetime.datetime.now())+"] Invite Group: "+gid+"/"+gname+"\n\n"+log 76 time.sleep(10) 77 cl.acceptGroupInvitation(gid) 78 print("Joined Group: "+gid+"/"+gname) 79 log = "["+str(datetime.datetime.now())+"] Joined Group: "+gid+"/"+gname+"\n\n"+log 80 if op.type == 24: 81 cl.log("[ 24 ] NOTIFIED LEAVE ROOM") 82 #mid = op.param1 83 #cl.leaveRoom(mid) 84 #print("Left Room: "+mid) 85 if op.type == 25: 86 cl.log("[ 25 ] SEND MESSAGE") 87 to = op.message.to 88 contentType = op.message.contentType 89 messageId = op.message.id 90 text = op.message.text 91 if contentType == 0: 92 if text is None: 93 return 94 if to.startswith("u"): 95 displayName = cl.getContact(to).displayName 96 print("Send Message: "+to+"/"+displayName+" "+messageId+" "+text) 97 log = "["+str(datetime.datetime.now())+"] Send Message: "+to+"/"+displayName+" "+messageId+" "+text+"\n\n"+log 98 if to.startswith("r"): 99 print("Send Message: Room "+to+" "+messageId+" "+text) 100 log = "["+str(datetime.datetime.now())+"] Send Message: Room "+to+" "+messageId+" "+text+"\n\n"+log 101 if to.startswith("c"): 102 name = cl.getGroup(to).name 103 print("Send Message: Group "+to+"/"+name+" "+messageId+" "+text) 104 log = "["+str(datetime.datetime.now())+"] Send Message: Group "+to+"/"+name+" "+messageId+" "+text+"\n\n"+log 105 if contentType == 7: 106 stickerId = op.message.contentMetadata["STKID"] 107 stickerVersion = op.message.contentMetadata["STKVER"] 108 packageId = op.message.contentMetadata["STKPKGID"] 109 if to.startswith("u"): 110 displayName = cl.getContact(to).displayName 111 print("Send Message: "+to+"/"+displayName+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId) 112 log = "["+str(datetime.datetime.now())+"] Send Message: "+to+"/"+displayName+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId+"\n\n"+log 113 if to.startswith("r"): 114 print("Send Message: Room "+to+" "+messageId+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId) 115 log = "["+str(datetime.datetime.now())+"] Send Message: Room "+to+" "+messageId+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId+"\n\n"+log 116 if to.startswith("c"): 117 name = cl.getGroup(to).name 118 print("Send Message: Group "+to+"/"+name+" "+messageId+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId) 119 log = "["+str(datetime.datetime.now())+"] Send Message: Group "+to+"/"+name+" "+messageId+" "+messageId+" "+stickerId+","+stickerVersion+","+packageId+"\n\n"+log 120 if text.lower() == "restart": 121 with open("line.log", "w", "utf-8", "ignore") as f: 122 f.write(log) 123 restartBOT() 124 if op.type == 26: 125 cl.log("[ 26 ] RECEIVE MESSAGE") 126 to = op.message.to 127 _from = op.message._from 128 messageId = op.message.id 129 text = op.message.text 130 if to == clMID: 131 displayName = cl.getContact(_from).displayName 132 cl.sendChatChecked(_from, messageId) 133 else: 134 name = cl.getGroup(to).name 135 displayName = cl.getContact(_from).displayName 136 cl.sendChatChecked(to, messageId) 137 if op.message.contentType == 0: 138 if "MENTION" in op.message.contentMetadata.keys() != None: 139 names = re.findall(r'@(\w+)', text) 140 mention = ast.literal_eval(op.message.contentMetadata["MENTION"]) 141 mentionees = mention["MENTIONEES"] 142 lists = [] 143 for mention in mentionees: 144 if clMID in mention["M"]: 145 if to == clMID: 146 print("Receive Message: "+_from+"/"+displayName+" "+messageId+" Mention") 147 log = "["+str(datetime.datetime.now())+"] Receive Message: "+_from+"/"+displayName+" "+messageId+" Mention"+"\n\n"+log 148 else: 149 print("Receive Message: "+to+"/"+name+" "+_from+"/"+displayName+" "+messageId+" Mention") 150 log = "["+str(datetime.datetime.now())+"] Receive Message: "+to+"/"+name+" "+_from+"/"+displayName+" "+messageId+" Mention"+"\n\n"+log 151 if text is None: 152 return 153 if to == clMID: 154 print("Receive Message: "+_from+"/"+displayName+" "+messageId+" "+text) 155 log = "["+str(datetime.datetime.now())+"] Receive Message: "+_from+"/"+displayName+" "+messageId+" "+text+"\n\n"+log 156 else: 157 print("Receive Message: "+to+"/"+name+" "+_from+"/"+displayName+" "+messageId+" "+text) 158 log = "["+str(datetime.datetime.now())+"] Receive Message: "+to+"/"+name+" "+_from+"/"+displayName+" "+messageId+" "+text+"\n\n"+log 159 if text.lower() == "restart": 160 restartBOT() 161 if op.type == 15: 162 cl.log("[ 15 ] MEMBER JOIN TO GROUP") 163 gid = op.param1 164 name = cl.getGroup(gid).name 165 if op.param2 in clMID: 166 print("Member Join To Group: "+gid+"/"+name+" Me") 167 log = "["+str(datetime.datetime.now())+"] Member Join To Group: "+gid+"/"+name+" Me"+"\n\n"+log 168 return 169 mid = op.param2 170 displayName = cl.getContact(mid),displayName 171 print("Member Join To Group: "+gid+"/"+name+" "+mid+"/"+displayName) 172 log = "["+str(datetime.datetime.now())+"] Member Join To Group: "+gid+"/"+name+" "+mid+"/"+displayName+"\n\n"+log 173 if op.type == 15: 174 cl.log("[ 15 ] MEMBER LEAVE TO GROUP") 175 mid = op.param2 176 gid = op.param1 177 name = cl.getGroup(gid).name 178 if mid == clMID: 179 print("Member Leave To Group: "+gid+"/"+name+" Me") 180 log = "["+str(datetime.datetime.now())+"] Member Leave To Group: "+gid+"/"+name+" Me"+"\n\n"+log 181 displayName = cl.getContact(mid).displayName 182 print("Member Leave To Group: "+gid+"/"+name+" "+mid+"/"+"displayName") 183 log = "["+str(datetime.datetime.now())+"] Member Leave To Group: "+gid+"/"+name+" "+mid+"/"+"displayName"+"\n\n"+log 184 with open("line.log", "w", encoding="UTF-8") as f: 185 f.write(log) 186 187 188 except Exception as e: 189 cl.log(e) 190 191while True: 192 try: 193 ops = oepoll.singleTrace(count=50) 194 if ops is not None: 195 for op in ops: 196 clBot(op) 197 oepoll.setRevision(op.revision) 198 except Exception as e: 199 cl.log(e) 200

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

otn

2020/01/14 11:36

エラーメッセージの意味が分からないということですか? エラー発見行の行番号が出ていませんか?
shiracamus

2020/01/14 11:37

どの行でエラーなのですか?
退会済みユーザー

退会済みユーザー

2020/01/14 11:40 編集

[2020-01-14 20:37:27.655335] an integer is required (got type str) [2020-01-14 20:37:27.655335] an integer is required (got type str) [2020-01-14 20:37:27.820091] an integer is required (got type str) [2020-01-14 20:37:27.820848] an integer is required (got type str) こんな感じで出てきます エラー行などは何故か出てきません
shiracamus

2020/01/14 11:44 編集

Pythonの実行エラーではなく、エラーメッセージを正常受信したのですか? 送信内容に問題があるということであれば、送信内容を全部printしてみてはいかがですか?
退会済みユーザー

退会済みユーザー

2021/11/15 02:13 編集

tryをなくしたら出てきました Traceback (most recent call last): File "c:/Users/****/Desktop/readBot.py", line 196, in <module> clBot(op) File "c:/Users/****/Desktop/readBot.py", line 58, in clBot with open("line.log","r", "utf-8", "ignore") as f: TypeError: an integer is required (got type str) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:/Users/****/Desktop/readBot.py", line 199, in <module> logError(e) NameError: name 'logError' is not defined
退会済みユーザー

退会済みユーザー

2020/01/14 11:59

どうやら open でエラーになっていると思われますが、 エラーの意味が分かりません
guest

回答3

0

open でエラーになっていると思われますが、

そうですね。

エラーの意味が分かりません

an integer is required (got type str)

require は 要求する
got は 得た(得るの過去形)
type は 型

という意味です。an, is, integer, str はわかりますよね。
訳すと「整数が要求されている。文字列型を得た」という意味です。

open関数の引数は、リファレンスを見ると、

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

第3引数は整数です。これに対して"utf-8"と文字列を与えていますので、「整数が要求されている。文字列型を得た」となります。
途中を省略せずに、open("line.log","r",-1,"utf-8","ignore")と書くか、仮引数名を書いてopen("line.log","r",encoding="utf-8",errors="ignore")とします。

長すぎるので、中身を全く見てませんでしたが、まだ一度もちゃんと動いていないプログラムの処理全体をtryの中に入れるのはあまり普通ではないです。

投稿2020/01/14 12:23

otn

総合スコア84555

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

引数を"encoding='utf-8'"等のキーワード付きにしてみたらどうでしょうか?

python

1with open("line.log","r", "utf-8", "ignore") as f:

投稿2020/01/14 12:10

meg_

総合スコア10580

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

openの引数を明記していませんでした

Python

1誤:with open("line.log", "r", "utf-8", "ignore") as f: 2正:with open("line.log", "r", encoding="utf-8", errors="ignore") as f:

解答、ありがとうございました

投稿2020/01/14 12:06

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問