【问题标题】:Sending Facebook message发送 Facebook 消息
【发布时间】:2019-12-31 02:01:53
【问题描述】:

只是想用 python 发送一条 Facebook 消息。

代码:

import fbchat

from fbchat import Client

from fbchat.models import *

client = Client("my_username", "my_password")

#help(fb.Client.send)

#client.send(text="This is a test", thread_id="christopher.batey", thread_type=ThreadType.USER)

name = "christopher.batey"

friends = client.searchForUsers(name)

friend = friends[0]

uid = friend.uid

msg = "This is a test"

print(help(client.send))

client.send(Message(text=msg, thread_id="christopher.batey", thread_type=ThreadType.USER))

错误:

Traceback(最近一次通话最后一次):

文件“main.py”,第 13 行,在

client.send(Message(text=msg, thread_id="christopher.batey", thread_type=ThreadType.USER))

TypeError: init() 得到了一个意外的关键字参数 'thread_id'

【问题讨论】:

  • Facebook 的服务条款规定您需要使用 API 与他们进行交互。 fbchat 库未使用 API。

标签: python python-3.x facebook


【解决方案1】:

几年前我花了一些时间探索这个模块。

快速回顾一下 github 示例,thread_id 应该是一个数字。

因此改变

client.send(Message(text=msg, thread_id="christopher.batey", thread_type=ThreadType.USER))

到

client.send(Message(text=msg), thread_id=uid, thread_type=ThreadType.USER)

在您的代码中。因为您的 uid 也是您要发送的朋友的。

【讨论】:

  • Traceback(最近一次调用最后):文件“main.py”,第 13 行,在 client.send(Message(text=msg), thread_id="uid", thread_type=ThreadType .USER)文件“/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/fbchat/_client.py”,第1107行,在发送返回self._doSendRequest(数据)文件“ /home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/fbchat/_client.py",第 1066 行,在 _doSendRequest j = self._post("/messaging/send/",数据)文件“/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/fbchat/_client.py”,li
  • 我认为这可能与您的朋友=朋友[0]有关,搜索不一定将最佳匹配放在首位。您是否尝试过打印朋友并查看第一个元素是什么。过去,我总是手动找到我需要的 uid,或者在向我发送某些内容时获取线程 id。
  • @ChristopherBatey 检查此问题的一种方法是将 thread_id 更改为 client.uid 而不仅仅是 uid 并查看消息是否发送给您自己 - 如果有效,那么您的 uid 有问题发现。
【解决方案2】:

我不熟悉 API,但从文档来看,正确的调用应该是:

client.send(Message(text=msg), thread_id="christopher.batey", thread_type=ThreadType.USER)

其中消息对象是传递给 send 的第一个参数,然后是 thread_id 和 thread_type。

【讨论】:

  • 通过此编辑会发生这种情况... Traceback(最近一次调用最后一次):文件“main.py”,第 13 行,在 client.send(Message(text=msg), thread_id ="uid", thread_type=ThreadType.USER) 文件“/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/fbchat/_client.py”,第 1107 行,在发送返回self._doSendRequest(data) 文件“/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/fbchat/_client.py”,第 1066 行,在 _doSendRequest j = self._post( “/messaging/send/”,数据)文件“/home/runner/.local/share/virtualenvs/python3/lib/python3.7/s
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 2013-10-28
相关资源
最近更新 更多