hibiyasleep / calcium

fetch/parse school meals list from goverment.
9 stars 2 forks source link

calcium-tweet bot #8

Closed mu-hun closed 7 years ago

mu-hun commented 7 years ago

Hello. I'm trying to make a twitter bot based on your module.

While i trying to make a twitter bot, I found a @SunrinMeal Twitter bot source code on your gist profile : https://gist.github.com/hibiyasleep/a29a1099147733d9d1e526d6671fa9c9

I modifie this source code and create the json file, but I did not get the OAuth to linking the twitter account. 2017-05-02 19-21-20

Could you tell me the structure of /home/hibiya/.calciumrc json file?

modified code

#!/usr/bin/env node

'use strict'

const calcium = require('calcium')
const moment = require('moment')
const fs = require('fs')
const T = require('twit')

const rc = JSON.parse(fs.readFileSync('calciumrc.json').toString())

let d = new Date()
let today = d.getDate()
let hour = d.getHours()

for(let id in rc.accounts) {
  let account = rc.accounts[id]
  let t = new T({
    consumer_key:         'consumer_key',
    consumer_secret:      'consumer_secret',
    access_token:         'access_token',
    access_token_secret:  'access_token_secret',
    timeout_ms:           60*1000,  // optional HTTP request timeout to apply to all requests.
    })

calciumrc.json code

{
    "account": {
        "id": {
            "school": "T100000124"
        }
    }
}
hibiyasleep commented 7 years ago

늦어서 죄송합니다.

calcium-tweet is intended to use with crontab, and supports multiple accounts. and please note, calcium-tweet just updated on gist.

accounts:
  screen_name: # your ID here
    school: SCHOOL_CODE_HERE # required
    feature: [...] # array of option string
    # private: append @_ @screen_name in front of tweet
    # gosam: add CSAT timer (...)
    filter: 'regex string' # remove all regex match from tweet
    timesIn: [12] # will only tweeted when executed at specified hour
    key:
      consumer_key: ~~~
      consumer_secret: ~~~
      access_token: ~~~
      access_token_secret: ~~~
  # (optional: more accounts here...)
hibiyasleep commented 7 years ago

Since calcium-tweet doesn't provide login option, you should generate access token yourself. I personally use sferik/t to make tokens.

mu-hun commented 7 years ago

Can i generate token with apps.twitter.com ?

hibiyasleep commented 7 years ago

Yes but limited to application's owner. see 'Keys and Access Tokens' tab on your application page.

ed) Make sure your application have r/w permission before generate access token.

mu-hun commented 7 years ago

consumer_key 를 읽을 수 없다고 실행이 안됩니다,, 모듈 측에서 잘못된 건지 모르겠네요.

error log

node_modules/twit/lib/twitter.js:46
    consumer_key        : config.consumer_key,
                                ^

TypeError: Cannot read property 'consumer_key' of undefined
    at new Twitter (/node_modules/twit/lib/twitter.js:46:33)
    at Object.<anonymous> (index.js:36:11)

.calciumrc

{
    "accounts": {
        "school": "T100000124",
        "key": {
            "consumer_key": ,
"consumer_secret": ,
            "access_token": ,
            "access_token_secret": 
        }
    }
}
hibiyasleep commented 7 years ago

twit 모듈이랑 같이 쓸 생각으로 config를 만들었기 때문에,

... = new T(config.key)

로 하시면 될 것 같습니다.

mu-hun commented 7 years ago

다시 확인해보니 config id 설정이 문제였습니다.

{
    "accounts": {
        "SeogwiMeal" : {
       }
    }
}

exit with code: 0

혹시 아래처럼 별다른 스케줄러 없이 forever을 사용해야 되는건가요?

 betafish@Z435  ~/Documents/github/heroku/seogobob   master  forever index.js start
warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Forever detected script exited with code: 0
error: Script restart attempt #1
error: Forever detected script exited with code: 0
error: Script restart attempt #2
error: Forever detected script exited with code: 0
error: Script restart attempt #3
error: Forever detected script exited with code: 0
hibiyasleep commented 7 years ago

node에 별로 쓸만한 스케쥴러가 없어서, forever 등으로 실행하는 대신 crontab에 붙이게 만들었습니다.

# 분    시      일      월      요일
# m     h       dom     mon     dow     command
00      00      *       *       1-5     /home/hibiya/cron/calcium-tweet
10      12      *       *       1-5     /home/hibiya/cron/calcium-tweet
40      17      *       *       1-5     /home/hibiya/cron/calcium-tweet