どのようにPythonとFacebookのチャット、非常に高速かつ簡単にボットを作成する方法.
17351 ワード
私は非常によく自分自身を説明しない場合は申し訳ありませんが、私も自分自身を理解していない知っている.lol
OK、私は最も速くて簡単なことをしたいので、このbotをするために、我々はPythonのモジュールを使います
イベントアクションは、サブクラスによって変更できます
これは
上記のコードの主なポイントを説明しましょう、MessageHoundオブジェクトは、チャットで受信された適切なメッセージは、オブジェクトのように参照してください、すべてのメッセージを送信するか、Facebookに受信するためにatributes , yeahはテキストだけでなく、この属性を持っています.
次は次のようになります.
そして最後に
そして、ここではボット作業.lolあなたは考えませんか?
それで、これは私の部分のすべてです、さよなら、animus、決してあきらめないで、努力し続けてください.
OK、私は最も速くて簡単なことをしたいので、このbotをするために、我々はPythonのモジュールを使います
fbchat
, コマンドでインストールできますpip
ですから、私のようなGNulinuxを使用して、パッケージマネージャが次のコマンドを実行するなら、次のコマンドを実行します.sudo apt install python-pip; sudo pip install fbchat
次は私たちのボットをビルドするために必要なすべてのものをインポートするので、最初のモジュールをインポートされますClient とfbchatのログとmodels , その後、モジュールgetpass 私たちは、このモジュールを必要としています.なぜなら、私たちがログインしているアカウントでボットにログインする必要があるからです.私たちがログインしているアカウントで、そのアカウントのチャットでボットとgetpass
モジュールは私たちのアカウントのパスワードを簡単かつ安全な方法を与えます.from fbchat import Client, log
from fbchat.models import *
from getpass import getpass
OKクラスのクライアントはfbchatのようないくつかのクールな方法がありますlisten()
and onMessage()
:listen()
This method initializes and runs the listening loop continually, this loop start to listen for events, we can define what should be executed or do the bot when certain events happen.
onMessage()
This method is called when the client is listening, and somebody sends a message, cool right?
イベントアクションは、サブクラスによって変更できます
Client()
, それからイベントメソッドを上書きする.これは
onMessage()
メソッド:
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
"""
Called when the client is listening, and somebody sends a message
:param author_id: The ID of the author
:param message_object: The message (As a `Message` object)
:param thread_id: Thread ID that the message was sent to. See :ref:`intro_threads`
:param thread_type: Type of thread that the message was sent to. See :ref:`intro_threads`
:type message_object: models.Message
:type thread_type: models.ThreadType
"""
log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name))
今のところ、このメソッドは何もしないので、いくつかの命令でこのメソッドを変更しましょう.もちろん、client ()メソッドとまったく同じように動作するサブクラスを作りますonMessage()
実行されるいくつかの追加命令を追加するメソッドは、メッセージ受信テキストに依存します.
from fbchat import log, Client
from fbchat.models import *
from getpass import getpass
class testBot(Client):
def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
"""
Called when the client is listening, and somebody sends a message
:param author_id: The ID of the author
:param message_object: The message (As a `Message` object)
:param thread_id: Thread ID that the message was sent to. See :ref:`intro_threads`
:param thread_type: Type of thread that the message was sent to. See :ref:`intro_threads`
:type message_object: models.Message
:type thread_type: models.ThreadType
"""
log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name))
msgText=message_object.text
msgText.lower()
if msgText == 'hi' or msgText == "hello":
self.send(Message(text="Hi my friend"), thread_id=thread_id,thread_type=thread_type )
elif msgText == 'love':
self.reactToMessage(message_object.uid, MessageReaction.LOVE)
elif msgText == 'peace':
self.reactToMessage(message_object.uid, MessageReaction.HEART)
self.send(Message(text="First you must have peace in your heart, to give peace to anyone"), thread_id=thread_id, thread_type=thread_type )
client=testBot(raw_input("Insert the email of facebook account"), getpass())
client.listen()
上のコードでは、すべての機能を固有の新しいクラスを作成していますClient()
メソッドを上書きするonMessage()
クライアントが聞いていてメッセージが受信されたときに呼び出されます.上記のコードの主なポイントを説明しましょう、MessageHoundオブジェクトは、チャットで受信された適切なメッセージは、オブジェクトのように参照してください、すべてのメッセージを送信するか、Facebookに受信するためにatributes , yeahはテキストだけでなく、この属性を持っています.
#: The actual message
text = None
#: A list of :class:`Mention` objects
mentions = None
#: A :class:`EmojiSize`. Size of a sent emoji
emoji_size = None
#: The message ID
uid = None
#: ID of the sender
author = None
#: Timestamp of when the message was sent
timestamp = None
#: Whether the message is read
is_read = None
#: A dict with user's IDs as keys, and their :class:`MessageReaction` as values
reactions = None
#: A :class:`Sticker`
sticker = None
#: A list of attachments
attachments = None
そこで、この部分では、受信したメッセージのテキストを変数msgtextに保存し、メソッドを使用しますlower()
すべてのテキストを小文字で変換するには、文字列の比較時に問題を回避します.msgText=message_object.text
msgText.lower()
次は、テキストに基づいた何かを受け取るコンディションです.if msgText == 'hi' or msgText == "hello":
self.send(Message(text="Hi my friend"), thread_id=thread_id,thread_type=thread_type )
elif msgText == 'love':
self.reactToMessage(message_object.uid, MessageReaction.LOVE)
elif msgText == 'peace':
self.reactToMessage(message_object.uid, MessageReaction.HEART)
self.send(Message(text="First you must have peace in your heart, to give peace to anyone"), thread_id=thread_id, thread_type=thread_type )
どうやって見るのif
msg受信のテキストが"Hi "か"Hello "の場合、BOTは"Hi my friend "というメッセージを返しますsend()
このメソッドは少なくとも値を必要とします、そして、それはThreadAdd IDです、スレッドは2つのものに言及することができます:メッセンジャーグループチャットまたは一つのFacebookユーザー、すべてのユーザーがこの部分のためにThreadRed IDと同じである自身のUIDを持っているFacebookでself.send(Message(text="Hi my friend"), thread_id=thread_id,thread_type=thread_type )
私は変数ThreadRange IDを使用してThreadRID IDの値を設定します.なぜなら、任意のメッセージを受信するときには、変数ThreadAid IDがMSGを送信するスレッドのThreadAdd IDで設定されています.次は次のようになります.
elif msgText == 'love':
self.reactToMessage(message_object.uid, MessageReaction.LOVE)
MSGテキストが「愛」であるならば、上記のコードでは、私たちのボットは、そのMSGをそれに反応させますreactToMessage()
メソッドは、すべてのメッセージオブジェクトに独自のIDを持ちます.UIDは受信msgのIDであるので、msgテキストが「Love」の場合、私たちのボットはどんなMSGに対しても愛反応に反応します.elif msgText == 'peace':
self.reactToMessage(message_object.uid, MessageReaction.HEART)
self.send(Message(text="First you must have peace in your heart, to give peace to anyone"), thread_id=thread_id, thread_type=thread_type )
上記のコードは、私たちのボットは、受信したメッセージのテキストが“平和”である場合は、受信メッセージに心と反応するつもりだ“最初に、あなたの心の中で平和を持って、誰に平和を与えるために”あなたの心に平和を持っている必要があります.そして最後に
client=testBot(raw_input("Insert the email of facebook account"), getpass())
client.listen()
Facebookに上記のコードのログイン、およびイベントを聞くためにボットを置く.そして、ここではボット作業.lolあなたは考えませんか?
それで、これは私の部分のすべてです、さよなら、animus、決してあきらめないで、努力し続けてください.
Reference
この問題について(どのようにPythonとFacebookのチャット、非常に高速かつ簡単にボットを作成する方法.), 我々は、より多くの情報をここで見つけました https://dev.to/titanhero/how-to-make-bot-for-the-facebook-chat-with-python-very-fast-and-easy-2bcgテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol