ba0f3 / telebot.nim

Async Telegram Bot API Client implement in @Nim-Lang
MIT License
165 stars 24 forks source link

Broken inline_keyboard.nim #13

Closed juancarlospaco closed 6 years ago

juancarlospaco commented 6 years ago

This example is broken: https://github.com/ba0f3/telebot.nim/blob/master/examples/inline_keyboard.nim

This line

let bot = newTeleBot(API_KEY, "nim_telebot")

Needs to be changed to:

let bot = newTeleBot(API_KEY)

But then it fails with:

inline_keyboard.nim(13, 21) Error: expression 'read(future523036)' has no type (or is ambiguous)

About this line:

updates = await bot.getUpdates(timeout=300)

:crying_cat_face:

juancarlospaco commented 6 years ago

Compiles but Crash on start:

import telebot, asyncdispatch, options, logging

const API_KEY = ""  # Add your API Key here.

addHandler(newConsoleLogger(fmtStr="$levelname, [$time] "))

proc handleUpdate(bot: TeleBot): UpdateCallback =
  proc cb(e: Update) {.async.} =
    var
      message = newMessage(e.message.get.chat.id, "Example Inline Keyboard.")
      home = initInlineKeyboardButton("Nim Homepage")
      forum = initInlineKeyboardButton("Nim forum")
      nimble = initInlineKeyboardButton("Nim Packages")
      telegram = initInlineKeyboardButton("Nim Telegram")

    home.url = "https://nim-lang.org".some
    forum.url = "https://forum.nim-lang.org".some
    nimble.url = "https://nimble.directory/".some
    telegram.url = "https://t.me/nim_lang".some

    message.replyMarkup = newInlineKeyboardMarkup(@[home, forum], @[nimble, telegram])

    discard await bot.send(message)
  result = cb

let
  bot = newTeleBot(API_KEY)
  handler = handleUpdate(bot)

bot.onUpdate(handler)
bot.poll(666)
juancarlospaco commented 6 years ago

@ba0f3

$ cat inline_keyboard.nim 

import telebot, asyncdispatch, logging, options

var L = newConsoleLogger()
addHandler(L)

const API_KEY = "" # I removed the key :)

var updates: seq[Update]

proc handleUpdate(bot: TeleBot): UpdateCallback =
  proc cb(e: Update) {.async.} =
    var response = e.message.get
    if response.text.isSome:
      let
        text = response.text.get
      var
        message = newMessage(response.chat.id, text)
        google = initInlineKeyboardButton("Google")
        bing = initInlineKeyboardButton("Bing")
        ddg = initInlineKeyboardButton("DuckDuckGo")
        searx = initInlineKeyboardButton("searx.me")

      google.url = some("https://www.google.com/search?q=" & text)
      bing.url = some("https://www.bing.com/search?q=" & text)
      ddg.url = some("https://duckduckgo.com/?q=" & text)
      searx.url = some("https://searx.me/?q=" & text)

      message.replyMarkup = newInlineKeyboardMarkup(@[google, bing], @[ddg, searx])
      discard await bot.send(message)
  result = cb

when isMainModule:
  let
    bot = newTeleBot(API_KEY)
    handler = handleUpdate(bot)

  bot.onUpdate(handler)
  bot.poll(300)

$ nim c -r -d:ssl inline_keyboard.nim 

inline_keyboard.nim(8, 5) Hint: 'updates' is declared but not used [XDeclaredButNotUsed]                                                                          
Hint:  [Link]                                                                                                                                                     
Hint: operation successful (69683 lines compiled; 1.950 sec total; 214.371MiB peakmem; Debug Build) [SuccessX]                                                    
Hint: /home/juan/code/nim-telegram-bot/src/inline_keyboard  [Exec]                                                                                                
DEBUG Making request to https://api.telegram.org/bot/getUpdates
DEBUG Result: [
  {
    "update_id": 321235416,
    "message": {
      "message_id": 92,
      "from": {
        "id": 26789038,
        "is_bot": false,
        "first_name": "Juan",
        "last_name": "Carlos",
        "username": "juancarlospaco",
        "language_code": "en-US"
      },
      "chat": {
        "id": 26789038,
        "first_name": "Juan",
        "last_name": "Carlos",
        "username": "juancarlospaco",
        "type": "private"
      },
      "date": 1533265527,
      "text": "dfsd"
    }
  },
  {
    "update_id": 321235417,
    "message": {
      "message_id": 93,
      "from": {
        "id": 26789038,
        "is_bot": false,
        "first_name": "Juan",
        "last_name": "Carlos",
        "username": "juancarlospaco",
        "language_code": "en-US"
      },
      "chat": {
        "id": 26789038,
        "first_name": "Juan",
        "last_name": "Carlos",
        "username": "juancarlospaco",
        "type": "private"
      },
      "date": 1533267861,
      "text": "foo"
    }
  }
]
Traceback (most recent call last)
inline_keyboard.nim(39)  inline_keyboard
api.nim(477)             poll
asyncdispatch.nim(1507)  waitFor
asyncdispatch.nim(1511)  poll
asyncdispatch.nim(1277)  runOnce
asyncdispatch.nim(183)   processPendingCallbacks
asyncmacro.nim(34)       getUpdates_continue
api.nim(447)             getUpdatesIter
asyncevents.nim(372)     emit
asyncevents.nim(345)     cb
asyncevents.nim(370)     emitIter
asyncmacro.nim(401)      cb
asyncmacro.nim(34)       cb_continue
inline_keyboard.nim(30)  cbIter
asyncmacro.nim(401)      send
asyncmacro.nim(34)       send_continue
api.nim(10)              sendIter
utils.nim(193)           addData
keyboard.nim(80)         $
json.nim(1155)           $
json.nim(1137)           toUgly
json.nim(1128)           toUgly
json.nim(1128)           toUgly
json.nim(1137)           toUgly
json.nim(1122)           toUgly
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/home/juan/code/nim-telegram-bot/src/inline_keyboard '

$
ba0f3 commented 6 years ago

example still works fine for me, please update again and compile with -f to clear cache