陳昀馨
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    5
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # python plurk api 2.0 噗浪機器人製作教學 這邊是噗浪機器人api2.0的製作教學文,包含回應,按愛心功能,還有簡易部屬,有興趣的人歡迎看看,若有錯誤也歡迎指教:D ## 首先先創一個新的噗浪帳號 ![](https://i.imgur.com/3uvAyeo.png =500x) 創建完成後,[點這裡](https://www.plurk.com/PlurkApp/),按下「註冊新的應用服務」, 接下來的步驟[這邊](https://dada.tw/2011/10/28/426/)很詳細,就不贅述了XD,我的機器人程式也參考了這個連結,在這裡你會獲得四個參數,請記起來 - App Key - App Secret - Access Token - Access Token Secret ## Python 安裝 如果是沒有安裝python的人,可以參考[教學](https://medium.com/ai-for-k12/python-%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8%E5%8F%8A%E8%AA%AA%E6%98%8E-a07de2463d98),我記得mac原本就有安裝python3的樣子,如果是mac使用者可以直接繼續看 ## 程式部分 可參考[這份專案](https://github.com/chenyunhsin/PlurkSealBot/blob/main/bot.py),這是SealBot的初期版本 可以選擇在terminal輸入此指令 `git@github.com:chenyunhsin/PlurkSealBot.git` (需要git帳號),或是直接複製起來,存在自己新建的檔案裡,接著依序輸入以下指令(//後面是註解,不用輸入) ``` cd seal-bot0930 //進入seal-bot0930這個資料夾 python3 -m venv venv //創建虛擬環境,這邊如果沒有下載會跳出提示,就照著指令下載就行 source venv/bin/activate //啟動環境 pip3 install -r req.txt //下載需要的package vim bot.py //開啟bot.py這個檔案, 將這幾個參數 API_KEY,API_SECRET,ACCESS_KEY,ACCESS_TOKEN 替換成上上個步驟獲得的4個參數 python3 bot.py //執行 ``` 如果成功跑起來,可以試試看用本帳加機器人好友,理論上如果有好友邀請,機器人會自動同意邀請,且發噗後會回應~~ 偶爾漏噗純屬正常,就api撈回來的資料本來就有少,目前還沒想到比較好的解決方法:p ## 程式功能介紹,請配合完整程式碼對照著看 #### 非完整程式碼,複製貼上不會動喔,這邊是介紹~完整程式碼在[這邊](https://github.com/chenyunhsin/PlurkSealBot/blob/main/bot.py) 程式主體是一個while loop,會不斷地跑,偵測有沒有新的噗會好友邀請,並對關鍵字做回應,這邊分段對程式介紹(非完整程式碼),若要客製化機器人可以看看~ ``` #這邊是認證的部分 def auth(): #這邊拿好友列表 def setFriendList(): #認證+初始化+拿資料 def initApi(): #回噗 def plurkResponse(pid,content): plurk.callAPI('/APP/Responses/responseAdd', {'plurk_id': pid, 'content': content, 'qualifier': ':'}) #成功跑起來會印的字~ print("Seal bot is working!") #拿自己(機器人帳號)的河道資料 data=plurk.callAPI('/APP/Users/me') #儲存好友清單,這邊會存好友清單到一個list裡,後面方便判斷是否為好友噗 setFriendList() #程式主體,就是這個while一直跑一直拿資料~ while True: #這邊是初始化,認證的部分,並拉資料回來 match = initApi() if match: rawdata = match.group(1) data = json.loads(rawdata) new_offset = data.get('new_offset', -1) msgs = data.get('data') #如果沒有新資料就繼續跑,不處理 if not msgs : continue #有新資料!來看看是什麼東西!一個msg代表一則噗的資料,詳細可以參考plurk文件,最後有付連結 for msg in msgs: #每一則噗浪都有專屬的id pid = msg.get('plurk_id') #每個使用者都有專屬的id user_id = msg.get('user_id') #這邊判斷是否在好友清單內,如果是才回應,不是就不處理~ if not user_id in friend_list: continue #回傳的資料如果是'new_plurk',也就是新的噗才會回應 if msg.get('type') == 'new_plurk': #content就是噗文內容,這邊可以根據內容來做處理 content = msg.get('content_raw') #這邊的意思就是,如果包含"好累",就會回傳"辛苦了(cozy)" if content.find("好累") != -1: plurkResponse(pid,'辛苦了(cozy)') #如果包含"--noreply",不會回應,機器人有不回應的需求可以加,使用情境像是當使用者希望某個噗不要被回就在噗文打關鍵字就不會觸發回應" elif content.find("--noreply")!= -1: print(":p") #這邊除了關鍵字判斷外,還有多了按愛心功能,多接了一個api來幫這個噗按愛心 elif content.find("畫")!=-1 or content.find("賀圖")!=-1: #按愛心api plurk.callAPI('/APP/Timeline/favoritePlurks', {'ids': "["+str(pid)+"]"}) plurkResponse(pid, '好棒喔(woot)(woot)(woot)') #這邊是其他回應,如果沒有觸發任何關鍵字,可以弄一個清單去random回覆使用者,這邊的清單存在random_list裡,可以隨自己喜歡修改~ else: random.shuffle(random_list) plurkResponse(pid,random_list[0]) #會跑到這邊代表不是新的噗浪 else: print("not new plurk") ``` ### 請再創一個Procfile,請注意無副檔名! 內容如圖,總之就是為了讓worker去執行bot.py檔案! ![](https://i.imgur.com/eq3G43K.png) ## 使用Heroku部屬(將於11月起停止免費方案) 這邊是選用[Heroku](https://dashboard.heroku.com/)來部屬,選擇原因是Heroku提供免費帳戶每個月450個小時的運行時間,開通信用卡後+550個小時的免費時數,如果只有一個server,是很夠用的,至少我目前還沒被收到錢啦XD 先創建帳號後,登入,點選create new app ![](https://i.imgur.com/jWakNax.png =800x) 點進剛剛創的app,然後點Deploy ![](https://i.imgur.com/YwoV6Ne.png =800x) 往下拉會看到一些指令,教你[下載cli](https://devcenter.heroku.com/articles/heroku-cli)(這邊請根據自己的系統來選喔)和如何上傳檔案到heroku的空間 ![](https://i.imgur.com/NeqThcO.png =800x) 上傳成功後,termianl輸入`heroku ps:scale worker=1`,因為雖然Procfile有定義,但根據heroku文件所說,除了web dyno,其他要自己起0.0 > Heroku runs one web dyno for you automatically, but other process types don’t start by default. To launch a worker, you need to scale it up to one dyno: 最後用此指令可以看log`heroku logs `,這個看dyno狀態`heroku ps` ## 使用Railway部屬 免費方案為每個月500小時5美元 ![](https://i.imgur.com/jkuN26L.png) 首先請於[github](https://github.com)開一個Private的專案,請務必開Private,因為機器人專案包含很多token! 創建頁面如圖, ![](https://i.imgur.com/2SLETiz.png) 再把小海豹的程式碼丟進這個專案,如果懶得git clone到local再push,就直接這邊新增就好~ ![](https://i.imgur.com/5qWvXq5.png) 再來到[Railway](https://railway.app/new)選擇Deploy from github repo後,綁定上面的專案 ![](https://i.imgur.com/0s3CxPi.png) 接下來去Dashboard找到剛建立好的Railway專案,在Start Command輸入 ``` python3 bot.py ``` 這邊是部署專案啟動時會下的command,我們只需要啟動機器人而已,因此這個command就夠了!至於requirements.txt,Railway環境會先自動pip install -r requirements.txt,所以不需要自己下! ![](https://i.imgur.com/fOcliWS.png) 結束了~目前還在試跑中,總之先看看會不會被停掉或收錢0.0 ## 注意事項 - 噗浪機器人無法貼連結,但課噗幣可以解決,之前卜卦功能突然失效,寄信去問才知道被系統判斷為機器人就會這樣(使用次數過多就會被判斷為機器人),官方回應如下供大家參考 ![](https://i.imgur.com/a4tr4ep.jpg =300x) ![](https://i.imgur.com/FSTY6OU.jpg =300x) - 其他還有轉噗,消音,加為粉絲,判斷被提到等功能,可以再參考[Plurk api 2.0](https://www.plurk.com/API) ## 參考 [Plurk oauth2](https://github.com/joestump/python-oauth2) [Plurk api 2.0](https://www.plurk.com/API) [dada](https://dada.tw/2011/10/28/426/) [豹豹機器人簡略版程式碼](https://github.com/chenyunhsin/PlurkSealBot/tree/main) ## 問題回報or建議or想抖內(? [請私噗](https://www.plurk.com/jktruimp1) ## 豹豹機器人連結 [他搭](https://www.plurk.com/SealBot)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully