iWIN 必須被解散!

Weil Jimmer's BlogWeil Jimmer's Blog


Category:Software

Found 11 records. At Page 2 / 3.

淺談Telegram開發機器人
No Comments

發布:
更新:2017-08-19 16:48:44

我做了一個很簡單的計時器,為了某群組裡面的高中生而設計,這也是我首次製作 tg 的機器人。我才做兩個而已,所以有些功能我真的不知道的我也沒辦法為您解答。

以下是講我的經歷:

首先必須先註冊個telegram帳號(廢話),然後搜索聯繫人@BotFather 這很重要,加入這個機器人之後,首先輸入:/newbot ,然後再輸入你想要取的名子(可以是中文),接著系統會提示你再輸入用戶名稱,結尾必須是 bot ,而且僅英文數字以及下畫線,接著你的機器人就創立了,BotFather會告訴你,你的Token,這很重要,因為等下傳送或是接收訊息都要用到這組文字。

開始就是程式設計的部分,我個人是比較熟PHP,所以用PHP開發。而且還是改寫自官方的腳本。但我現在已經明白原理了。

<?php

define('BOT_TOKEN', 'BotFather給你的Token');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');

//這些AppFunction和處理資料有關係,我是覺得直接Copy就好了。
function apiRequestWebhook($method, $parameters) {
  if (!is_string($method)) {
    error_log("Method name must be a string\n");
    return false;
  }

  if (!$parameters) {
    $parameters = array();
  } else if (!is_array($parameters)) {
    error_log("Parameters must be an array\n");
    return false;
  }

  $parameters["method"] = $method;

  header("Content-Type: application/json");
  echo json_encode($parameters);
  return true;
}

function exec_curl_request($handle) {
  $response = curl_exec($handle);

  if ($response === false) {
    $errno = curl_errno($handle);
    $error = curl_error($handle);
    error_log("Curl returned error $errno: $error\n");
    curl_close($handle);
    return false;
  }

  $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
  curl_close($handle);

  if ($http_code >= 500) {
    // do not wat to DDOS server if something goes wrong
    sleep(10);
    return false;
  } else if ($http_code != 200) {
    $response = json_decode($response, true);
    error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
    if ($http_code == 401) {
      throw new Exception('Invalid access token provided');
    }
    return false;
  } else {
    $response = json_decode($response, true);
    if (isset($response['description'])) {
      error_log("Request was successfull: {$response['description']}\n");
    }
    $response = $response['result'];
  }

  return $response;
}

function apiRequest($method, $parameters) {
  if (!is_string($method)) {
    error_log("Method name must be a string\n");
    return false;
  }

  if (!$parameters) {
    $parameters = array();
  } else if (!is_array($parameters)) {
    error_log("Parameters must be an array\n");
    return false;
  }

  foreach ($parameters as $key => &$val) {
    // encoding to JSON array parameters, for example reply_markup
    if (!is_numeric($val) && !is_string($val)) {
      $val = json_encode($val);
    }
  }
  $url = API_URL.$method.'?'.http_build_query($parameters);

  $handle = curl_init($url);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($handle, CURLOPT_TIMEOUT, 60);

  return exec_curl_request($handle);
}

function apiRequestJson($method, $parameters) {
  if (!is_string($method)) {
    error_log("Method name must be a string\n");
    return false;
  }

  if (!$parameters) {
    $parameters = array();
  } else if (!is_array($parameters)) {
    error_log("Parameters must be an array\n");
    return false;
  }

  $parameters["method"] = $method;

  $handle = curl_init(API_URL);
  curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($handle, CURLOPT_TIMEOUT, 60);
  curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($parameters));
  curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

  return exec_curl_request($handle);
}

//上面的程式碼都建議直接Copy拿來用

//我自己的程式碼開始

$target_times=1465772400;
$msg = '距離【畢業典禮】還有'."\n".floor(($target_times-time())/86400).'天'.floor((($target_times-time())%86400)/3600).'時'.floor(((($target_times-time())%86400)%3600)/60).'分'.(((($target_times-time())%86400)%3600)%60).'秒!';

if (time()>$target_times){
	$msg="";
}

$target_times=1467327600;
$msg .= "\n".'距離【指考】還有'."\n".floor(($target_times-time())/86400).'天'.floor((($target_times-time())%86400)/3600).'時'.floor(((($target_times-time())%86400)%3600)/60).'分'.(((($target_times-time())%86400)%3600)%60).'秒!';

if (time()>$target_times){
	$msg="";
}


//處理開始函數,傳入$msg是因為我這個程式的功能所需,並非必要
function processMessage($message,$msg) {
  // process incoming message
  $message_id = $message['message_id'];
  $chat_id = $message['chat']['id'];
  if (isset($message['text'])) {
    // incoming text message
    $text = $message['text'];

    if ($text === "/start" or $text === "/start@botusername"){
		if ($msg!=""){
			apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => $msg."\n聊天室ID:".$chat_id));
		}
    } else if ($text === "/show" or $text === "/show@botusername") {
		if ($msg!=""){
			apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => $msg));
		}
    } else if (strpos($text, "/stop") === 0) {
	// stop now
    } else {
	// 其他指令或訊息
    }
  }
}

if (php_sapi_name() == 'cli') {
  // if run from console
  //這是為了定時任務的區別,如果是從Crontab跑,就從這邊
	if ($msg!=""){
		apiRequestJson("sendMessage", array('chat_id' => '00000000000', "text" => $msg));
	}
  exit;
}


$content = file_get_contents("php://input"); //讀取資料 (In)
$update = json_decode($content, true); //JSON解析

if (!$update) {
  // receive wrong update, must not happen
  // 接收到錯誤訊息
  exit;
}

if (isset($update["message"])) {
  //接收到正確訊息,調用處理函數
  processMessage($update["message"],$msg);
}

程式碼都獻上了,現在開始講原理。

首先必須要有一台服務器,來執行WebHook,把你寫好的PHP檔案上傳到伺服器上面,注意檔名最好建立很特殊的名稱,讓一般使用者無法猜到你用什麼檔名,以預防惡意人士直接訪問你的程式。(洪水轟炸,或偽造請求之類的。)

上傳後,請直接訪問 https://api.telegram.org/bot{TOKEN}/?method=setWebhook&url=https://www.example.com/{secret}bot.php

例如:
https://api.telegram.org/bot123456789:AAAAAAAAAAAAAAAAAAAAAA/?method=setWebhook&url=https://www.example.com/secret_random_path/bot.php

參數url後面接的就是你要接收的位置,僅支援https,完畢後一旦直接對bot講話輸入指令,telegram就會立刻送出對話內容傳到你的程式,而你的程式處理完之後再回傳給telegram,這就是它的工作原理。

得到的內容長得像:(已經被解析的Object["message"])

(
    [message_id] => 01
    [from] => Array
        (
            [id] => 123456789
            [first_name] => Nickname
            [username] => username
        )

    [chat] => Array
        (
            [id] => -123456789
            [title] => test
            [type] => group
        )

    [date] => 1464504645
    [text] => /say I just say?
    [entities] => Array
        (
            [0] => Array
                (
                    [type] => bot_command
                    [offset] => 0
                    [length] => 17
                )

        )

)

簡單明瞭,這只是message的部分,有可能會出現其他資料,例如傳圖片,有人加入群組時,就會不同了,請參閱 telegram 文檔,https://core.telegram.org/bots/api

而我的設計是當使用者輸入 /start 時,就會回傳 聊天室 id,因為我需要 id 才可以讓我直接發訊息回去,否則必須每次都是被動型傳輸,這樣我就可以主動傳送了,因為我不需要依靠對方先傳訊息給我才可以取得 id 回傳。

而我設定輸入指令 /show 時 就會彈出距離目標時間還有多久。而且利用 cron job 定時任務,每天都會 run 這程式發送一次倒數訊息。這就是整個簡單的概念。

接下來,在 tg 上就可以直接操作並直接顯示結果了,唯一缺陷就是沒有指令選單。這時可以再向 BotFather設定,輸入 /setcommands,再輸入 @botname指令(英文)+空格+簡介。(可以多行輸入)

最終預防機器人被濫用亂加入群組,可以設定鎖定,/setjoingroups@botname → Disable

這樣就大功告成了。其他功能自己探索,其實很簡單的。


This entry was posted in General, Software, Experience, Functions, Note, PHP By Weil Jimmer.

音樂播放程式 Media List Viewer
No Comments

發布:
更新:2017-11-17 00:52:47

本程式可以選擇特定資料夾目錄並列舉資料夾內所有音樂,以資料夾為第一級/或子資料夾分類,並包含搜尋功能(可自訂資料夾內搜尋),置頂功能(一律顯示在最上方)。只是個簡單版的輕巧小程式。

本程式已過時,請改用:http://web.wbftw.org/product/newmediaviewerplus

下載地址【一】:https://url.weils.net/m

下載地址【二】:http://cht.tw/h/p9bxu

產品頁面:http://web.wbftw.org/product/yinlebofangchengshimedialistview


This entry was posted in General, Software, Free, Product By Weil Jimmer.

最棒的手機通訊軟體 Telegram
3 Comments

發布:
更新:2017-05-19 14:33:13

安全聊天室

最大的特點是:Telegram 支持 RSA 2048 AES 256 的安全端對端加密通訊,這是我認為最棒的功能,它可以幫助用戶阻擋外人侵入,竊取你們的通訊,而Line、FB、Yahoo……等通訊軟體,基本上,聲稱有安全加密,問題是:那些供應商是不是就可以看你的對話!

Telegram 的獨特 秘密聊天室 可以確保用戶的通訊安全,不必擔心講機密事情被警察監聽,被政府監聽,連Telegram自己都不能解密,所以,你們之間的通訊只有在你們之間才可以看得到!

除此之外, Telegram 還支持 秘密訊息 自我銷毀 的功能,超過一定時間就會自動毀滅文檔,是指 雙方的資料同步消失,若正在聊天中,用戶主動刪除訊息,而對方那邊也會跟著同步刪除,非常酷炫!

各平台/裝置支援度高

而且 Telegram 這款應用程式支援所有平台,IOS、Android、Windows、Linux 通通支援!不論手機還是電腦版,非常方便,甚至可以同時登入!還有更離譜的功能是可以自己傳訊息給自己。

容量大、永久保存的雲端

Telegram 所提供出來的雲端空間呢,是無限空間無限流量的,但是單個檔案上限大小是 1.5 GB ,意思是不可以上傳過大的檔案,同時,不論手機或電腦版,都可以傳輸檔案,傳輸照片"原檔",而非壓縮,傳輸任意格式的檔案,不像 Line 只有電腦版才可以上傳檔案,手機板無法傳檔案,聊天室圖片轉寄還會越轉越失真,轉越多人越模糊。真的很爛。

在線狀態/列表

而且在聊天室中,打字或錄音或上傳檔案,都會被顯示在狀態列,還有上線列表可以查看最後上線日期,所以可以知道誰可能已讀了,也可以知道誰正在打字中、上傳檔案中…。當然也可以隱藏最後上線時間。

管理員

在Telegram裡面,還有所謂的管理員,可以決定用戶的加入與踢出,決定群組圖片、名稱,同理也可以設定人人都是管理員,但,創始人是無法被剔除的。

同步刪除

超級群組中,會多出獨特的功能就是「可以編輯訊息」、「可以刪除訊息」,刪除訊息?什麼意思,本來不就可以刪了嗎?新版的更新後,現在已經可以在私聊中刪除自己的對話(短期內),對方也會消失,在超級群組中,自己刪掉自己的訊息,是大家都會消失掉的!

Telegram 的壓縮品質也較佳,圖片比較清晰,如果還嫌模糊就傳原檔吧!

純熟的機器人API

另外,它還可以支援 「機器人」,例如投票機器人、GIF搜圖機器人、調查機器人…,可以在聊天室中打指令實現某些功能,這個就讓用戶們自己去探索拉!

官網:https://telegram.org/

缺點

Telegram 的缺點有點少,首先是影片不可以邊播放邊下載

加好友方式,得用電話簿的方法加入,必須要知道好友的電話號碼。

缺點…其實我實在想不出有啥麼缺點。


This entry was posted in Software, Experience, Free, The Internet By Weil Jimmer.

SoundCloud Downloader EXE
No Comments

發布:
更新:2017-05-19 18:44:58

本程式可批量下載 soundcloud.com 的音樂,下載該名藝術家的所有音樂並編號,或是下載某播放清單的所有音樂,或是下載單首音樂。

The Application is able to mutiple download the music from "soundcloud.com" ,  you can also download all the song from the artists you love , and you can download specific playlist or tracks.

2017.02.15 - 修復Bug - 無法下載問題。

版本(Version):1.0.0.1

Last update:2017.02.16

下載地址【一】:https://url.weils.net/l

下載地址【二】:https://url.weils.net/q

產品頁面:http://web.wbftw.org/product/soundclouddownloaderexe


This entry was posted in General, Software, Free, The Internet, Product, Tools By Weil Jimmer.

日記程式 下載
No Comments

發布:
更新:2018-04-14 22:30:30

獨特的加密,「寫日記程式」,免費下載與使用。

這是我親自開發的程式之一,也是我目前覺得寫得還算正式的產品。看網路上很多日記程式都很假,我用過,他們那些都是「假藉」有密碼的前提,實際上,只不過是封鎖了要讀取的地方而已,我也可以用筆記本打開那些日記檔案,結果發現都沒有「加密」!

沒有加密,那開啟的密碼也只是虛設了!趕快來使用本團隊研發出來最安全的日記程式!

主要功能說明:

一、自訂文字顏色屬性、置中、置左、置右,可以插入圖片,還有插入附件檔案(連同檔案一塊加密存成一個日記檔)。

二、支持自訂背景音樂,包括音樂列表循環播放,音量控制,自動播放、順序調整之類的。

三、日記列表方便管理,具有搜尋功能,重新命名、編輯列表,刪除檔案,讀取檔案…等。

四、真正具有加密/解密效果的日記,不必擔心日記外洩。即便獲取原始檔案,依舊無法解密,解密需要"密碼"。(如果忘記就永遠解不出來了)

五、會詳細的紀錄目前的時間、目前日記的字數、插入了多少圖片、多少檔案(甚至可以計算所有日記加起來的字數、圖片數、檔案數)。

六、可自訂背景圖片,背景顏色,文字顏色、打字框顏色……的。

*註一:日記程式的背景圖片和背景音樂都不會經過加密,只是把圖片和音樂檔移動到資料夾內而已。

*註二:日記程式所創之密碼Hash檔已被加密過,即便解密,也只是一長串被加鹽運算一萬次的密文,幾乎無從還原。

下載地址【一】:https://url.weils.net/p

下載地址【二】:http://cht.tw/h/7i19i

2018/03/28 更新:可以嘗試使用本站開發的手機板日記程式。

https://my.pcloud.com/publink/show?code=kZwY2NZ5wWIfrfX3DQXWBx8DTN3GfTt90Y7#folder=1583580893


This entry was posted in Android, Software, Free, Product, Tools, VB.NET By Weil Jimmer.

上一頁  1 2 3 /3 頁)下一頁

Visitor Count

pop
nonenonenone

Note

支持網路中立性.
Support Net Neutrality.

飽暖思淫欲,饑寒起盜心。

支持臺灣實施無條件基本收入

歡迎前來本站。

Words Quiz


Search

Music

Blogging Journey

4249days

since our first blog post.

Republic Of China
The strong do what they can and the weak suffer what they must.

Privacy is your right and ability to be yourself and express yourself without the fear that someone is looking over your shoulder and that you might be punished for being yourself, whatever that may be.

It is quality rather than quantity that matters.

I WANT Internet Freedom.

Reality made most of people lost their childishness.

Justice,Freedom,Knowledge.

Without music life would be a mistake.

Support/Donate

This site also need a little money to maintain operations, not entirely without any cost in the Internet. Your donations will be the best support and power of the site.
MethodBitcoin Address
bitcoin1gtuwCjjVVrNUHPGvW6nsuWGxSwygUv4x
buymeacoffee
Register in linode via invitation link and stay active for three months.Linode

Support The Zeitgeist Movement

The Zeitgeist Movement

The Lie We Live

The Lie We Live

The Questions We Never Ask

The Questions We Never Ask

Man

Man

THE EMPLOYMENT

Man

In The Fall

In The Fall

Facebook is EATING the Internet

Facebook

Categories

Android (7)

Announcement (4)

Arduino (2)

Bash (2)

C (3)

C# (5)

C++ (1)

Experience (50)

Flash (2)

Free (13)

Functions (36)

Games (13)

General (57)

HTML (7)

Java (13)

JS (7)

Mood (24)

Note (30)

Office (1)

OpenWrt (5)

PHP (9)

Privacy (4)

Product (12)

Python (4)

Software (11)

The Internet (24)

Tools (16)

VB.NET (8)

WebHosting (7)

Wi-Fi (5)

XML (4)