bot_telegram_api
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
send_telegram_messages_via_api [2021/02/26 16:07] – atolstov | bot_telegram_api [2021/03/11 09:30] – atolstov | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Send Telegram messages to multiply | + | ====== Send via Telegram |
<WRAP center round alert 60%> | <WRAP center round alert 60%> | ||
Nota bene, \\ | Nota bene, \\ | ||
Line 41: | Line 41: | ||
<code lua - TelegramChatBot.lua> | <code lua - TelegramChatBot.lua> | ||
- | local botToken = " | ||
- | |||
- | local bot_id = tonumber(string.sub(botToken, | ||
- | |||
function main (userId) | function main (userId) | ||
+ | local botToken = " | ||
+ | local bot_id = tonumber(string.sub(botToken, | ||
+ | |||
+ | | ||
+ | lastSentTime = R(" | ||
- | now = os.time(); | + | -- function main (userId) |
- | local message = " | + | |
- | local condition_to_send = (tonumber(os.date(" | + | |
- | + | local message = " | |
- | | + | |
- | if (R(" | + | |
- | first_request_no_chat_id = true | + | |
- | chat_id = "" | + | |
- | else | + | -- check if it is initialisation or not |
- | first_request_no_chat_id = false | + | if (R(" |
- | chat_id = R(" | + | first_request_no_chat_id = true |
- | end | + | |
- | + | -- return | |
- | -- initialisation branch | + | |
- | if first_request_no_chat_id | + | |
- | local initialisation_request_ulr = ' | + | chat_id = R(" |
- | DEBUG(initialisation_request_ulr ) | + | end |
- | c = cURL.easy_init() | + | if first_request_no_chat_id then TRACE("first_request_no_chat_id |
- | c: | + | |
- | c: | + | -- initialisation |
- | local res = "" | + | if first_request_no_chat_id |
- | c: | + | local initialisation_request_ulr |
- | writefunction = function(str) | + | DEBUG(initialisation_request_ulr |
- | res = res .. str; | + | |
- | end}) | + | |
- | DEBUG(res) | + | |
- | local table = cjson.decode(res) | + | |
- | --- Check when bot is invited to group | + | |
- | if table.result[1].message.new_chat_participant.id == bot_id then | + | |
- | new_chat_id = tostring(table.result[1].message.chat.id) | + | |
- | WriteReg(" | + | |
- | end | + | |
- | -- work branch | + | |
- | else | + | |
- | + | ||
- | lastSentTime = R(" | + | |
- | -- local condition_to_send = (tonumber(os.date(" | + | |
- | if condition_to_send | + | |
- | local url = ' | + | |
- | + | ||
- | local res = ""; | + | |
c = cURL.easy_init() | c = cURL.easy_init() | ||
- | c:setopt_url(url) | + | c:setopt_timeout(5) |
- | | + | |
+ | local res = "" | ||
c:perform({ | c:perform({ | ||
writefunction = function(str) | writefunction = function(str) | ||
- | | + | |
- | end}) | + | end}) |
- | + | | |
local table = cjson.decode(res) | local table = cjson.decode(res) | ||
- | local error_code | + | --- Check when bot is invited to group |
+ | for jj, jvalue in pairs ( table.result) do | ||
+ | if tonumber(table.result[jj].my_chat_member.chat.id) < 0 then | ||
+ | new_chat_id | ||
+ | WriteReg(" | ||
+ | break | ||
+ | end | ||
+ | end | ||
| | ||
- | | + | -- work branch |
- | error_code = table.error_code | + | else |
- | lastErrorCode = error_code | + | |
- | lastSentTime = tonumber(os.date(" | + | lastSentTime = R(" |
- | -- DEBUG(lastErrorCode) | + | -- local condition_to_send = (tonumber(os.date(" |
- | WriteReg(" | + | if condition_to_send == true then |
- | + | local url = ' | |
- | else | + | |
- | lastSentTime = tonumber(os.date(" | + | local res = ""; |
- | lastSentTimeSuccesful = lastSentTime | + | c = cURL.easy_init() |
- | lastStatus = table.result.message_id | + | c: |
- | + | --INFO(message); | |
- | WriteReg(" | + | c: |
+ | writefunction = function(str) | ||
+ | res = res .. str; | ||
+ | end}) | ||
+ | |||
+ | local table = cjson.decode(res) | ||
+ | local error_code = nil | ||
+ | |||
+ | | ||
+ | error_code = table.error_code | ||
+ | lastErrorCode = error_code | ||
+ | lastSentTime = tonumber(os.date(" | ||
+ | -- DEBUG(lastErrorCode) | ||
+ | WriteReg(" | ||
+ | | ||
+ | else | ||
+ | lastSentTime = tonumber(os.date(" | ||
+ | lastSentTimeSuccesful = lastSentTime | ||
+ | lastStatus = table.result.message_id | ||
+ | | ||
+ | WriteReg(" | ||
+ | end | ||
+ | c:close() | ||
+ | WriteReg(" | ||
+ | WriteReg(" | ||
end | end | ||
- | c:close() | ||
- | WriteReg(" | ||
- | WriteReg(" | ||
end | end | ||
- | end | + | |
+ | |||
+ | |||
end | end | ||
- | function urlencode(str) | + | |
- | if (str) then | + | if (str) then |
- | str = string.gsub (str, " | + | str = string.gsub (str, " |
- | str = string.gsub (str, "([^%w ])", | + | str = string.gsub (str, "([^%w ])", |
- | function (c) return string.format (" | + | function (c) return string.format (" |
- | str = string.gsub (str, " ", " | + | str = string.gsub (str, " ", " |
+ | end | ||
+ | return str | ||
end | end | ||
- | return str | ||
- | end | ||
</ | </ | ||
Line 134: | Line 149: | ||
At the rest, invite that bot to your chat. | At the rest, invite that bot to your chat. | ||
+ | |||
+ | <WRAP center round tip 60%> | ||
+ | Do not forget to enable bot access to messages in BotFather' | ||
+ | </ | ||
+ | |||
Run script and it will initialise. In the '' | Run script and it will initialise. In the '' | ||
+ | |||
+ | <WRAP center round download 60%> | ||
+ | {{ : | ||
+ | </ | ||
+ |
bot_telegram_api.txt · Last modified: 2021/03/11 09:31 by atolstov