Python Facebookchat AttributeError ‘str’ object has no attribute ‘_to_send_data’

huangapple go评论64阅读模式
英文:

Python Facebookchat AttributeError 'str' object has no attribute '_to_send_data'

问题

以下是您要翻译的内容:

我正在尝试使用Python给我的朋友发送消息。
但是我遇到了这个错误。

sent = client.send(friend.uid, msg)
File "/home/can/anaconda3/lib/python3.7/site-packages/fbchat/_client.py", line 1059, in send
    data.update(message._to_send_data())
AttributeError: 'str' object has no attribute '_to_send_data'

我可以登录我的账户,也可以输入朋友的值,还可以输入我的朋友名字。

然后,当我写下我的消息,例如“hello”,然后按下回车,它会给我报错。

程序代码如下:

import fbchat
from getpass import getpass
username = str(input("Username: "))
client = fbchat.Client(username, getpass())
no_of_friends = int(input("Number of friends: "))
for i in range(no_of_friends):
    name = str(input("Name: "))
    friends = client.searchForUsers(name)  # 返回一个名字列表
    friend = friends[0]
    msg = str(input("Message: "))
    sent = client.send(friend.uid, msg)
    if sent:
        print("Message sent successfully!")

我能看到“Message sent successfully!”这个消息吗?另外,模块已成功安装。

我正在使用Ubuntu 19.10。

英文:

I was trying send message to my friend using python.
But I am getting this error.

   sent = client.send(friend.uid, msg)
  File "/home/can/anaconda3/lib/python3.7/site-packages/fbchat/_client.py", line 1059, in send
    data.update(message._to_send_data())
AttributeError: 'str' object has no attribute '_to_send_data'

I can login in to my account and ı can enter value of friends also ı can enter my friend name.

Then when ı write my message for example "hello" then press enter, it gives me error.

Codes of program;

import fbchat
from getpass import getpass
username = str(input("Username: "))
client = fbchat.Client(username, getpass())
no_of_friends = int(input("Number of friends: "))
for i in range(no_of_friends):
    name = str(input("Name: "))
    friends = client.searchForUsers(name)  # return a list of names
    friend = friends[0]
    msg = str(input("Message: "))
    sent = client.send(friend.uid, msg)
    if sent:
        print("Message sent successfully!")

can I see "Message sent successfully!" this message? also modules are successfully installed.

I am working on Ubuntu 19.10

答案1

得分: 0

我自己解决了我的问题。我把答案放在这里,也许有人会遇到相同的错误,可以在这里找到答案。

我的错误在这里:

sent = client.send(friend.uid, msg)

我改成了这样:

sent = client.sendMessage(msg, thread_id=friend.uid)

而且它起作用了!

英文:

I fix my issue by myself. ı put the answer here maybe someone will have same error and can find the answer here.

My mistake is here;

sent = client.send(friend.uid, msg)

I changed like this one;

sent = client.sendMessage(msg, thread_id=friend.uid)

and it's worked!

huangapple
  • 本文由 发表于 2020年1月6日 23:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定